SlideShare a Scribd company logo
Threads : Java
Prepared By Tuhin Kundu
What is a Thread?
facility to allow multiple activities to coexist within a
single process.
Represents a separate path of execution of a group of
statements
Java is the first language to include threading within the
language, rather than treating it as a facility of the OS
 Video Game example
1.one thread for graphics
2.one thread for user interaction
3.one thread for networking
 Server Example
1.Do various jobs
2.Handle Several Clients
Main Thread
Default Thread in any Java Program
JVM uses to execute program statements
o Program To Find the Main Thread
Class Current
{
public static void main(String args[])
{
Thread t=Thread.currentThread();
System.out.println(“Current Thread: “+t);
System.out.println(“Name is: “+t.getName());
}
}
Threads in Java
Creating threads in Java:
Extend java.lang.Thread class
run() method must be overridden (similar to main method
of sequential program)
run() is called when execution of the thread begins
A thread terminates when run() returns
start() method invokes run()
OR
Implement java.lang.Runnable interface
4
Life cycle of a Thread
 New
The thread is in new state if you create an instance
of Thread class but before the invocation of start()
method.
 Runnable
The thread is in runnable state after invocation of start() method,
but the thread scheduler has not selected it to be the running
thread.
 Running
The thread is in running state if the thread scheduler has
selected it.
 Non-Runnable (Blocked)
This is the state when the thread is still alive, but is currently not
eligible to run.
 Terminated
A thread is in terminated or dead state when its run() method
exits.
Thread Priority
 Each thread is assigned a default priority of
Thread.NORM_PRIORITY (constant of 5).
 You can reset the priority using setPriority(int priority).
 Some constants for priorities include:
o Thread.MIN_PRIORITY
o Thread.MAX_PRIORITY
o Thread.NORM_PRIORITY
 By default, a thread has the priority level of the thread that
created it.
7
 Thread Synchronization8
A shared resource may be corrupted if it is accessed
simultaneously by multiple threads.
Example: two unsynchronized threads accessing the same
bank account may cause conflict.
 Known as a race condition in multithreaded programs.
A thread-safe class does not cause a race condition in the
presence of multiple threads.
Synchronized
Problem : race conditions
Solution : give exclusive access to one thread at a time to code
that manipulates a shared object.
Synchronization keeps other threads waiting until the object is
available.
The synchronized keyword synchronizes the method so that only one
thread can access the method at a time.
public synchronized void xMethod() {
// method body
}
Obj t1 ( Enters the object )
t2—wait until t1 comes out
9
Deadlock :
a part of multithreading
can occur when a thread is waiting for an object lock, that
is acquired by another thread and second thread is waiting
for an object lock that is acquired by first thread
Since, both threads are waiting for each other to release
the lock, the condition is called deadlock
Preventing Deadlock
Deadlock can be easily avoided by resource ordering.
With this technique, assign an order on all the objects
whose locks must be acquired and ensure that the
locks are acquired in that order.
Example:
Thread 1:
lock A lock B
Thread 2:
wait for A lock C(when A is locked)
Thread 3:
wait for A wait for B wait for C
11
Advantages of Threads:
easier to program
provide better performance
allow any program to perform multiple tasks at once.
multiple threads can share resources
an Internet-aware language such as Java, this is a very
important tool
References
 http://www.slideshare.net/parag/multithreading-in-java
 https://code.google.com/p/googleappengine/wiki/SdkForJavaReleaseNotes
 http://stackoverflow.com/questions/2213340/what-is-daemon-thread-in-java
 https://github.com/orientechnologies/orientdb/wiki/Java-Multi-Threading
 http://www.javatpoint.com/creating-thread
 http://www.tutorialspoint.com/java/java_multithreading.htm
 http://tutorials.jenkov.com/java-concurrency/creating-and-starting-threads.html
 http://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html
 http://www.javabeginner.com/learn-java/java-threads-tutorial
 http://www.geeksforgeeks.org/java/
 http://www.javacodegeeks.com/2014/08/java-concurrency-tutorial-visibility-between-
threads.html
End of Module
Thank You
Ad

More Related Content

What's hot (20)

Applets in java
Applets in javaApplets in java
Applets in java
Wani Zahoor
 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread Synchronization
Benj Del Mundo
 
Life cycle-of-a-thread
Life cycle-of-a-threadLife cycle-of-a-thread
Life cycle-of-a-thread
javaicon
 
Packages in java
Packages in javaPackages in java
Packages in java
Elizabeth alexander
 
Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
Shraddha
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
parag
 
Java awt (abstract window toolkit)
Java awt (abstract window toolkit)Java awt (abstract window toolkit)
Java awt (abstract window toolkit)
Elizabeth alexander
 
Type casting in java
Type casting in javaType casting in java
Type casting in java
Farooq Baloch
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
BHUVIJAYAVELU
 
Interface in java
Interface in javaInterface in java
Interface in java
PhD Research Scholar
 
Java Collections
Java  Collections Java  Collections
Java Collections
Kongu Engineering College, Perundurai, Erode
 
OOP java
OOP javaOOP java
OOP java
xball977
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
Shubham Dwivedi
 
Java thread life cycle
Java thread life cycleJava thread life cycle
Java thread life cycle
Archana Gopinath
 
Java package
Java packageJava package
Java package
CS_GDRCST
 
Thread model in java
Thread model in javaThread model in java
Thread model in java
AmbigaMurugesan
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
Pratik Soares
 
Packages in java
Packages in javaPackages in java
Packages in java
Kavitha713564
 
Java multi threading
Java multi threadingJava multi threading
Java multi threading
Raja Sekhar
 
Control structures in java
Control structures in javaControl structures in java
Control structures in java
VINOTH R
 

Similar to Threads in JAVA (20)

Java Threads
Java ThreadsJava Threads
Java Threads
Hamid Ghorbani
 
Multi-threaded Programming in JAVA
Multi-threaded Programming in JAVAMulti-threaded Programming in JAVA
Multi-threaded Programming in JAVA
Vikram Kalyani
 
Java unit 12
Java unit 12Java unit 12
Java unit 12
Shipra Swati
 
Multithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of threadMultithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of thread
Kartik Dube
 
Threads and synchronization in C# visual programming.pptx
Threads and synchronization in C# visual programming.pptxThreads and synchronization in C# visual programming.pptx
Threads and synchronization in C# visual programming.pptx
azkamurat
 
Java Multithreading Interview Questions PDF By ScholarHat
Java Multithreading Interview Questions PDF By ScholarHatJava Multithreading Interview Questions PDF By ScholarHat
Java Multithreading Interview Questions PDF By ScholarHat
Scholarhat
 
Multithreading and concurrency in android
Multithreading and concurrency in androidMultithreading and concurrency in android
Multithreading and concurrency in android
Rakesh Jha
 
Multithread Programing in Java
Multithread Programing in JavaMultithread Programing in Java
Multithread Programing in Java
M. Raihan
 
Concept of Java Multithreading-Partially.pptx
Concept of Java Multithreading-Partially.pptxConcept of Java Multithreading-Partially.pptx
Concept of Java Multithreading-Partially.pptx
SahilKumar542
 
Programming - Java-Threads-and-Synchronization.ppt
Programming - Java-Threads-and-Synchronization.pptProgramming - Java-Threads-and-Synchronization.ppt
Programming - Java-Threads-and-Synchronization.ppt
smychung
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Kavitha713564
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Kavitha713564
 
topic_presentation_java_threads_example.ppt
topic_presentation_java_threads_example.ppttopic_presentation_java_threads_example.ppt
topic_presentation_java_threads_example.ppt
ShitalBahale2
 
java threads for easy learn and develop
java  threads for easy learn and developjava  threads for easy learn and develop
java threads for easy learn and develop
pavanrock859
 
Threads in Java
Threads in JavaThreads in Java
Threads in Java
HarshaDokula
 
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
nimbalkarvikram966
 
Java class 6
Java class 6Java class 6
Java class 6
Edureka!
 
Threadnotes
ThreadnotesThreadnotes
Threadnotes
Himanshu Rajput
 
Multithreading
MultithreadingMultithreading
Multithreading
backdoor
 
Md09 multithreading
Md09 multithreadingMd09 multithreading
Md09 multithreading
Rakesh Madugula
 
Multi-threaded Programming in JAVA
Multi-threaded Programming in JAVAMulti-threaded Programming in JAVA
Multi-threaded Programming in JAVA
Vikram Kalyani
 
Multithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of threadMultithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of thread
Kartik Dube
 
Threads and synchronization in C# visual programming.pptx
Threads and synchronization in C# visual programming.pptxThreads and synchronization in C# visual programming.pptx
Threads and synchronization in C# visual programming.pptx
azkamurat
 
Java Multithreading Interview Questions PDF By ScholarHat
Java Multithreading Interview Questions PDF By ScholarHatJava Multithreading Interview Questions PDF By ScholarHat
Java Multithreading Interview Questions PDF By ScholarHat
Scholarhat
 
Multithreading and concurrency in android
Multithreading and concurrency in androidMultithreading and concurrency in android
Multithreading and concurrency in android
Rakesh Jha
 
Multithread Programing in Java
Multithread Programing in JavaMultithread Programing in Java
Multithread Programing in Java
M. Raihan
 
Concept of Java Multithreading-Partially.pptx
Concept of Java Multithreading-Partially.pptxConcept of Java Multithreading-Partially.pptx
Concept of Java Multithreading-Partially.pptx
SahilKumar542
 
Programming - Java-Threads-and-Synchronization.ppt
Programming - Java-Threads-and-Synchronization.pptProgramming - Java-Threads-and-Synchronization.ppt
Programming - Java-Threads-and-Synchronization.ppt
smychung
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Kavitha713564
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Kavitha713564
 
topic_presentation_java_threads_example.ppt
topic_presentation_java_threads_example.ppttopic_presentation_java_threads_example.ppt
topic_presentation_java_threads_example.ppt
ShitalBahale2
 
java threads for easy learn and develop
java  threads for easy learn and developjava  threads for easy learn and develop
java threads for easy learn and develop
pavanrock859
 
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
nimbalkarvikram966
 
Java class 6
Java class 6Java class 6
Java class 6
Edureka!
 
Multithreading
MultithreadingMultithreading
Multithreading
backdoor
 
Ad

Recently uploaded (20)

Degree_of_Automation.pdf for Instrumentation and industrial specialist
Degree_of_Automation.pdf for  Instrumentation  and industrial specialistDegree_of_Automation.pdf for  Instrumentation  and industrial specialist
Degree_of_Automation.pdf for Instrumentation and industrial specialist
shreyabhosale19
 
15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...
IJCSES Journal
 
Level 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical SafetyLevel 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical Safety
JoseAlbertoCariasDel
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
π0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalizationπ0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalization
NABLAS株式会社
 
International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)
samueljackson3773
 
Data Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptxData Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptx
RushaliDeshmukh2
 
Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...
Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...
Process Parameter Optimization for Minimizing Springback in Cold Drawing Proc...
Journal of Soft Computing in Civil Engineering
 
new ppt artificial intelligence historyyy
new ppt artificial intelligence historyyynew ppt artificial intelligence historyyy
new ppt artificial intelligence historyyy
PianoPianist
 
Introduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptxIntroduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptx
AS1920
 
The Gaussian Process Modeling Module in UQLab
The Gaussian Process Modeling Module in UQLabThe Gaussian Process Modeling Module in UQLab
The Gaussian Process Modeling Module in UQLab
Journal of Soft Computing in Civil Engineering
 
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptxExplainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
MahaveerVPandit
 
Oil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdfOil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdf
M7md3li2
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
some basics electrical and electronics knowledge
some basics electrical and electronics knowledgesome basics electrical and electronics knowledge
some basics electrical and electronics knowledge
nguyentrungdo88
 
railway wheels, descaling after reheating and before forging
railway wheels, descaling after reheating and before forgingrailway wheels, descaling after reheating and before forging
railway wheels, descaling after reheating and before forging
Javad Kadkhodapour
 
Raish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdfRaish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdf
RaishKhanji
 
Artificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptxArtificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptx
aditichinar
 
Smart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptxSmart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptx
rushikeshnavghare94
 
Metal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistryMetal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistry
mee23nu
 
Degree_of_Automation.pdf for Instrumentation and industrial specialist
Degree_of_Automation.pdf for  Instrumentation  and industrial specialistDegree_of_Automation.pdf for  Instrumentation  and industrial specialist
Degree_of_Automation.pdf for Instrumentation and industrial specialist
shreyabhosale19
 
15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...15th International Conference on Computer Science, Engineering and Applicatio...
15th International Conference on Computer Science, Engineering and Applicatio...
IJCSES Journal
 
Level 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical SafetyLevel 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical Safety
JoseAlbertoCariasDel
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
π0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalizationπ0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalization
NABLAS株式会社
 
International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)International Journal of Distributed and Parallel systems (IJDPS)
International Journal of Distributed and Parallel systems (IJDPS)
samueljackson3773
 
Data Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptxData Structures_Introduction to algorithms.pptx
Data Structures_Introduction to algorithms.pptx
RushaliDeshmukh2
 
new ppt artificial intelligence historyyy
new ppt artificial intelligence historyyynew ppt artificial intelligence historyyy
new ppt artificial intelligence historyyy
PianoPianist
 
Introduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptxIntroduction to Zoomlion Earthmoving.pptx
Introduction to Zoomlion Earthmoving.pptx
AS1920
 
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptxExplainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
Explainable-Artificial-Intelligence-XAI-A-Deep-Dive (1).pptx
MahaveerVPandit
 
Oil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdfOil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdf
M7md3li2
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
some basics electrical and electronics knowledge
some basics electrical and electronics knowledgesome basics electrical and electronics knowledge
some basics electrical and electronics knowledge
nguyentrungdo88
 
railway wheels, descaling after reheating and before forging
railway wheels, descaling after reheating and before forgingrailway wheels, descaling after reheating and before forging
railway wheels, descaling after reheating and before forging
Javad Kadkhodapour
 
Raish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdfRaish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdf
RaishKhanji
 
Artificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptxArtificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptx
aditichinar
 
Smart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptxSmart_Storage_Systems_Production_Engineering.pptx
Smart_Storage_Systems_Production_Engineering.pptx
rushikeshnavghare94
 
Metal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistryMetal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistry
mee23nu
 
Ad

Threads in JAVA

  • 1. Threads : Java Prepared By Tuhin Kundu
  • 2. What is a Thread? facility to allow multiple activities to coexist within a single process. Represents a separate path of execution of a group of statements Java is the first language to include threading within the language, rather than treating it as a facility of the OS  Video Game example 1.one thread for graphics 2.one thread for user interaction 3.one thread for networking  Server Example 1.Do various jobs 2.Handle Several Clients
  • 3. Main Thread Default Thread in any Java Program JVM uses to execute program statements o Program To Find the Main Thread Class Current { public static void main(String args[]) { Thread t=Thread.currentThread(); System.out.println(“Current Thread: “+t); System.out.println(“Name is: “+t.getName()); } }
  • 4. Threads in Java Creating threads in Java: Extend java.lang.Thread class run() method must be overridden (similar to main method of sequential program) run() is called when execution of the thread begins A thread terminates when run() returns start() method invokes run() OR Implement java.lang.Runnable interface 4
  • 5. Life cycle of a Thread
  • 6.  New The thread is in new state if you create an instance of Thread class but before the invocation of start() method.  Runnable The thread is in runnable state after invocation of start() method, but the thread scheduler has not selected it to be the running thread.  Running The thread is in running state if the thread scheduler has selected it.  Non-Runnable (Blocked) This is the state when the thread is still alive, but is currently not eligible to run.  Terminated A thread is in terminated or dead state when its run() method exits.
  • 7. Thread Priority  Each thread is assigned a default priority of Thread.NORM_PRIORITY (constant of 5).  You can reset the priority using setPriority(int priority).  Some constants for priorities include: o Thread.MIN_PRIORITY o Thread.MAX_PRIORITY o Thread.NORM_PRIORITY  By default, a thread has the priority level of the thread that created it. 7
  • 8.  Thread Synchronization8 A shared resource may be corrupted if it is accessed simultaneously by multiple threads. Example: two unsynchronized threads accessing the same bank account may cause conflict.  Known as a race condition in multithreaded programs. A thread-safe class does not cause a race condition in the presence of multiple threads.
  • 9. Synchronized Problem : race conditions Solution : give exclusive access to one thread at a time to code that manipulates a shared object. Synchronization keeps other threads waiting until the object is available. The synchronized keyword synchronizes the method so that only one thread can access the method at a time. public synchronized void xMethod() { // method body } Obj t1 ( Enters the object ) t2—wait until t1 comes out 9
  • 10. Deadlock : a part of multithreading can occur when a thread is waiting for an object lock, that is acquired by another thread and second thread is waiting for an object lock that is acquired by first thread Since, both threads are waiting for each other to release the lock, the condition is called deadlock
  • 11. Preventing Deadlock Deadlock can be easily avoided by resource ordering. With this technique, assign an order on all the objects whose locks must be acquired and ensure that the locks are acquired in that order. Example: Thread 1: lock A lock B Thread 2: wait for A lock C(when A is locked) Thread 3: wait for A wait for B wait for C 11
  • 12. Advantages of Threads: easier to program provide better performance allow any program to perform multiple tasks at once. multiple threads can share resources an Internet-aware language such as Java, this is a very important tool
  • 13. References  http://www.slideshare.net/parag/multithreading-in-java  https://code.google.com/p/googleappengine/wiki/SdkForJavaReleaseNotes  http://stackoverflow.com/questions/2213340/what-is-daemon-thread-in-java  https://github.com/orientechnologies/orientdb/wiki/Java-Multi-Threading  http://www.javatpoint.com/creating-thread  http://www.tutorialspoint.com/java/java_multithreading.htm  http://tutorials.jenkov.com/java-concurrency/creating-and-starting-threads.html  http://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html  http://www.javabeginner.com/learn-java/java-threads-tutorial  http://www.geeksforgeeks.org/java/  http://www.javacodegeeks.com/2014/08/java-concurrency-tutorial-visibility-between- threads.html