SlideShare a Scribd company logo
1 of 33
M U L T I T H R E A D I N
G
I N T R O D U C T I O N T O T H R E A D
• Process and Thread are two basic units of the program execution.
• A process is something that is currently under execution. So, an active
program can be called a Process.
• A thread is a sub-division of a process. Thread is a single sequence
stream within a process.
• play a very significant role in the faster execution of a process.
• A process is sub-divided into sub-tasks called threads, so multiple threads
function parallel (parallel processing) and is termed as Multi-threading.
S I N G L E T H R E A D E D P R O C E S S
M U LT I - T H R E A D E D P R O C E S S
M U LT I TA S K I N G
D E F I N I T I O N
• Multitasking allows a user to perform more than one computer task
simultaneously. Multiple tasks are also known as processes that share similar
processing resources like a CPU.
• Multiprogramming: When multiple programs execute at a time on a single
device, it is multiprogramming.
• Multitasking: When a single resource is used to process multiple tasks then it is
multitasking.
• Multithreading: It is an extended form of multitasking.
• Multiprocessing: When more than one processing unit is used by a single
computer then it is called multiprocessing.
T Y P E S
L I F E C Y C L E O F A T H R E A D
1.NEW – a newly created thread that has not yet started the execution
2.RUNNABLE – ready for execution but it's waiting for resource allocation
3.RUNNING _When the thread gets the CPU, it moves from the runnable to
the running state.
4.BLOCKED – waiting to acquire a monitor lock to enter or re-enter a
synchronized block/method
5.WAITING – waiting for some other thread to perform a particular action
without any time limit
6.TERMINATED – has completed its execution
M E T H O D S U S E D I N
M U LT I T H R E A D I N G
C R E AT I N G T H E T H R E A D S
• There are two ways to create a thread:
1.By extending Thread class
2.By implementing Runnable interface.
C O M M O N LY U S E D M E T H O D S O F
T H R E A D C L A S S :
public void run(): is used to perform action for a thread.
public void start(): starts the execution of the thread.JVM calls the run() method on the thread.
public void sleep(long miliseconds): Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of
milliseconds.
public void join(): waits for a thread to die.
public int getPriority(): returns the priority of the thread.
public int setPriority(int priority): changes the priority of the thread.
public boolean isAlive(): tests if the thread is alive.
public void yield(): causes the currently executing thread object to temporarily pause and allow other threads to execute.
public void suspend(): is used to suspend the thread(depricated).
public void resume(): is used to resume the suspended thread(depricated).
public void stop(): is used to stop the thread(depricated).
public boolean isDaemon(): tests if the thread is a daemon thread.
C O N S T R U C T O R S O F T H R E A D
C L A S S :
• Thread()
• Thread(String name)
• Thread(Runnable r)
• Thread(Runnable r,String name)
T H R E A D C L A S S :
• Thread class provide constructors and methods to create and perform
operations on a thread.
• Thread class extends Object class and implements Runnable interface.
C R E AT I N G T H E T H R E A D S
• There are two ways to create a thread:
1.By extending Thread class
2.By implementing Runnable interface.
1 . E X T E N D I N G T H R E A D C L A S S
class Multi extends Thread{
public void run(){
System.out.println("thread is running...");
}
public static void main(String args[]){
Multi t1=new Multi();
t1.start();
}
}
2 . I M P L E M E N T I N G R U N N A B L E
I N T E R FA C E
class Multi implements Runnable{
public void run(){
System.out.println("thread is running...");
}
public static void main(String args[]){
Multi m1=new Multi();
Thread t1 =new Thread(m1); // Using the constructor Thread(Runnable r)
t1.start();
}
}
• If we are not extending the Thread class, your class object would not be
treated as a thread object. So you need to explicitly create the Thread
class object. We are passing the object of your class that implements
Runnable so that your class run() method may execute.
• By implementing Runnable interface is a better way to create a
thread in java because when we create a thread by extending
Thread class, all Thread class methods are inherited while we
can perform the task with the one method (run method) only. It
results into overhead inheritance.
• Other reason is that java does not support multiple inheritance
in case of classes. So if we create a thread by extending the
Thread class, we will not be able to extend any other class.
• Thread class is in Java.lang package.
• run() method will contain the code for created thread.
• you can not directly call run method to start a thread. You need to call
start method to create a new thread.
• We can call run method directly. Only difference is that when start method
is called it creates a separate call stack for that thread but in case when
run method is called directly from main method it will not create a new call
stack.
T H R E A D P R I O R I T Y
• Each thread has a priority. Priorities are represented by a number
between 1 and 10.
• The default priority is set to 5 as excepted.
• Minimum priority is set to 1.
• Maximum priority is set to 10.
3 constants defined in Thread class:
1.public static int MIN_PRIORITY
2.public static int NORM_PRIORITY
3.public static int MAX_PRIORITY
T H E A C C E P T E D V A L U E O F P R I O R I T Y F O R A
T H R E A D I S I N T H E R A N G E O F 1 T O 1 0 .
get and set priority of a thread in java.
1.public final int getPriority(): java.lang.Thread.getPriority() method returns priority of given
thread.
2.public final void setPriority(int newPriority): java.lang.Thread.setPriority() method
changes the priority of thread to the value newPriority. This method throws
IllegalArgumentException if value of parameter newPriority goes beyond minimum(1) and
maximum(10) limit.
• currentThread() method to get the name of the current thread.
• setName() method to make names of thread as per choice for understanding purposes.
• getName() method will be used to get the name of the thread.
S Y N C H R O N I Z AT I O N I N J AVA
• Synchronization in Java is the capability to control the access of multiple
threads to any shared resource.
• Java Synchronization is better option where we want to allow only one
thread to access the shared resource.
• The synchronization is mainly used to
1.To prevent thread interference.
2.To prevent consistency problem.
T H R E A D S Y N C H R O N I Z AT I O N
• There are two types of thread synchronization mutual exclusive and inter-
thread communication.
1.Mutual Exclusive (no two processes can exist in the critical section at any
given point of time)
2.Cooperation (Inter-thread communication in java)
I N T E R -T H R E A D C O M M U N I C A T I O N :
• Inter-thread communication is a process in which a thread is
paused running in its critical region and another thread is
allowed to enter (or lock) in the same critical region to be
executed. i.e. synchronized threads communicate with each
other.
O B J E C T C L A S S M E T H O D S A R E U S E D F O R
I N T E R - T H R E A D C O M M U N I C A T I O N P R O C E S S
• wait(): this method instructs the current thread to release the monitor held
by it and to get suspended until some other threads sends a notification from
the same monitor.
• Syntax: public void wait() throws InterruptedException.
• notify(): this method is used to send the notification to the thread that is
suspended by the wait() method.
• Syntax: public void notify().
• notifyAll(): this method is used to send the notification to all the threads
that are suspended by wait() method.
• Syntax: public void notifyAll().
P R O D U C E R - C O N S U M E R P R O B L E M T O
U N D E R S T A N D I N T E R - T H R E A D
C O M M U N I C A T I O N .
• The producer-consumer problem (also known as the bounded-buffer
problem) is a classic example of a multi-process synchronization problem.
The problem describes two processes, the producer and the consumer,
which share a common, fixed-size buffer used as a queue.
• The producer’s job is to generate data, put it into the buffer, and start
again.
• At the same time, the consumer is consuming the data (i.e. removing it
from the buffer), one piece at a time.
• Problem
To make sure that the producer won’t try to add data into the buffer if it’s
full and that the consumer won’t try to remove data from an empty buffer.
• Solution
The producer is to either go to sleep or discard data if the buffer is full.
The next time the consumer removes an item from the buffer, it notifies
the producer, who starts to fill the buffer again. In the same way, the
consumer can go to sleep if it finds the buffer to be empty. The next time
the producer puts data into the buffer, it wakes up the sleeping
consumer.

More Related Content

Similar to multithreading.pptx

econtent thread in java.pptx
econtent thread in java.pptxecontent thread in java.pptx
econtent thread in java.pptxramyan49
 
PROGRAMMING IN JAVA-unit 3-part II
PROGRAMMING IN JAVA-unit 3-part IIPROGRAMMING IN JAVA-unit 3-part II
PROGRAMMING IN JAVA-unit 3-part IISivaSankari36
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaKavitha713564
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaKavitha713564
 
OOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptxOOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptxArulmozhivarman8
 
Inter thread communication & runnable interface
Inter thread communication & runnable interfaceInter thread communication & runnable interface
Inter thread communication & runnable interfacekeval_thummar
 
Multithreading in Java
Multithreading in JavaMultithreading in Java
Multithreading in JavaJayant Dalvi
 
Sync, async and multithreading
Sync, async and multithreadingSync, async and multithreading
Sync, async and multithreadingTuan Chau
 
JAVA THEORY PPT.pptx on based up on the transaction
JAVA THEORY PPT.pptx on based up on the transactionJAVA THEORY PPT.pptx on based up on the transaction
JAVA THEORY PPT.pptx on based up on the transactionSaikiranBiradar3
 
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptxnimbalkarvikram966
 
Unit-3 MULTITHREADING-2.pdf
Unit-3 MULTITHREADING-2.pdfUnit-3 MULTITHREADING-2.pdf
Unit-3 MULTITHREADING-2.pdfGouthamSoma1
 
Multithreading programming in java
Multithreading programming in javaMultithreading programming in java
Multithreading programming in javaElizabeth alexander
 
1.17 Thread in java.pptx
1.17 Thread in java.pptx1.17 Thread in java.pptx
1.17 Thread in java.pptxTREXSHyNE
 

Similar to multithreading.pptx (20)

econtent thread in java.pptx
econtent thread in java.pptxecontent thread in java.pptx
econtent thread in java.pptx
 
Md09 multithreading
Md09 multithreadingMd09 multithreading
Md09 multithreading
 
advanced java ppt
advanced java pptadvanced java ppt
advanced java ppt
 
Threads in Java
Threads in JavaThreads in Java
Threads in Java
 
PROGRAMMING IN JAVA-unit 3-part II
PROGRAMMING IN JAVA-unit 3-part IIPROGRAMMING IN JAVA-unit 3-part II
PROGRAMMING IN JAVA-unit 3-part II
 
Java threading
Java threadingJava threading
Java threading
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
OOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptxOOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptx
 
Inter thread communication & runnable interface
Inter thread communication & runnable interfaceInter thread communication & runnable interface
Inter thread communication & runnable interface
 
Multithreading in Java
Multithreading in JavaMultithreading in Java
Multithreading in Java
 
Sync, async and multithreading
Sync, async and multithreadingSync, async and multithreading
Sync, async and multithreading
 
JAVA THEORY PPT.pptx on based up on the transaction
JAVA THEORY PPT.pptx on based up on the transactionJAVA THEORY PPT.pptx on based up on the transaction
JAVA THEORY PPT.pptx on based up on the transaction
 
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
 
Java threads
Java threadsJava threads
Java threads
 
Unit-3 MULTITHREADING-2.pdf
Unit-3 MULTITHREADING-2.pdfUnit-3 MULTITHREADING-2.pdf
Unit-3 MULTITHREADING-2.pdf
 
Multithreading programming in java
Multithreading programming in javaMultithreading programming in java
Multithreading programming in java
 
7. Multithreading
7. Multithreading7. Multithreading
7. Multithreading
 
1.17 Thread in java.pptx
1.17 Thread in java.pptx1.17 Thread in java.pptx
1.17 Thread in java.pptx
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 

Recently uploaded

Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsNbelano25
 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactisticshameyhk98
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxPooja Bhuva
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfstareducators107
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111GangaMaiya1
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17Celine George
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptxJoelynRubio1
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
latest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answerslatest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answersdalebeck957
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 

Recently uploaded (20)

Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactistics
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
latest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answerslatest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answers
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 

multithreading.pptx

  • 1. M U L T I T H R E A D I N G
  • 2. I N T R O D U C T I O N T O T H R E A D • Process and Thread are two basic units of the program execution. • A process is something that is currently under execution. So, an active program can be called a Process. • A thread is a sub-division of a process. Thread is a single sequence stream within a process. • play a very significant role in the faster execution of a process. • A process is sub-divided into sub-tasks called threads, so multiple threads function parallel (parallel processing) and is termed as Multi-threading.
  • 3. S I N G L E T H R E A D E D P R O C E S S
  • 4. M U LT I - T H R E A D E D P R O C E S S
  • 5. M U LT I TA S K I N G
  • 6. D E F I N I T I O N • Multitasking allows a user to perform more than one computer task simultaneously. Multiple tasks are also known as processes that share similar processing resources like a CPU. • Multiprogramming: When multiple programs execute at a time on a single device, it is multiprogramming. • Multitasking: When a single resource is used to process multiple tasks then it is multitasking. • Multithreading: It is an extended form of multitasking. • Multiprocessing: When more than one processing unit is used by a single computer then it is called multiprocessing.
  • 7. T Y P E S
  • 8. L I F E C Y C L E O F A T H R E A D
  • 9. 1.NEW – a newly created thread that has not yet started the execution 2.RUNNABLE – ready for execution but it's waiting for resource allocation 3.RUNNING _When the thread gets the CPU, it moves from the runnable to the running state. 4.BLOCKED – waiting to acquire a monitor lock to enter or re-enter a synchronized block/method 5.WAITING – waiting for some other thread to perform a particular action without any time limit 6.TERMINATED – has completed its execution
  • 10. M E T H O D S U S E D I N M U LT I T H R E A D I N G
  • 11. C R E AT I N G T H E T H R E A D S • There are two ways to create a thread: 1.By extending Thread class 2.By implementing Runnable interface.
  • 12. C O M M O N LY U S E D M E T H O D S O F T H R E A D C L A S S : public void run(): is used to perform action for a thread. public void start(): starts the execution of the thread.JVM calls the run() method on the thread. public void sleep(long miliseconds): Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds. public void join(): waits for a thread to die. public int getPriority(): returns the priority of the thread. public int setPriority(int priority): changes the priority of the thread. public boolean isAlive(): tests if the thread is alive. public void yield(): causes the currently executing thread object to temporarily pause and allow other threads to execute. public void suspend(): is used to suspend the thread(depricated). public void resume(): is used to resume the suspended thread(depricated). public void stop(): is used to stop the thread(depricated). public boolean isDaemon(): tests if the thread is a daemon thread.
  • 13. C O N S T R U C T O R S O F T H R E A D C L A S S : • Thread() • Thread(String name) • Thread(Runnable r) • Thread(Runnable r,String name)
  • 14. T H R E A D C L A S S : • Thread class provide constructors and methods to create and perform operations on a thread. • Thread class extends Object class and implements Runnable interface.
  • 15.
  • 16. C R E AT I N G T H E T H R E A D S • There are two ways to create a thread: 1.By extending Thread class 2.By implementing Runnable interface.
  • 17. 1 . E X T E N D I N G T H R E A D C L A S S
  • 18. class Multi extends Thread{ public void run(){ System.out.println("thread is running..."); } public static void main(String args[]){ Multi t1=new Multi(); t1.start(); } }
  • 19. 2 . I M P L E M E N T I N G R U N N A B L E I N T E R FA C E
  • 20. class Multi implements Runnable{ public void run(){ System.out.println("thread is running..."); } public static void main(String args[]){ Multi m1=new Multi(); Thread t1 =new Thread(m1); // Using the constructor Thread(Runnable r) t1.start(); } }
  • 21. • If we are not extending the Thread class, your class object would not be treated as a thread object. So you need to explicitly create the Thread class object. We are passing the object of your class that implements Runnable so that your class run() method may execute.
  • 22. • By implementing Runnable interface is a better way to create a thread in java because when we create a thread by extending Thread class, all Thread class methods are inherited while we can perform the task with the one method (run method) only. It results into overhead inheritance. • Other reason is that java does not support multiple inheritance in case of classes. So if we create a thread by extending the Thread class, we will not be able to extend any other class.
  • 23. • Thread class is in Java.lang package. • run() method will contain the code for created thread. • you can not directly call run method to start a thread. You need to call start method to create a new thread. • We can call run method directly. Only difference is that when start method is called it creates a separate call stack for that thread but in case when run method is called directly from main method it will not create a new call stack.
  • 24. T H R E A D P R I O R I T Y • Each thread has a priority. Priorities are represented by a number between 1 and 10. • The default priority is set to 5 as excepted. • Minimum priority is set to 1. • Maximum priority is set to 10.
  • 25. 3 constants defined in Thread class: 1.public static int MIN_PRIORITY 2.public static int NORM_PRIORITY 3.public static int MAX_PRIORITY
  • 26. T H E A C C E P T E D V A L U E O F P R I O R I T Y F O R A T H R E A D I S I N T H E R A N G E O F 1 T O 1 0 . get and set priority of a thread in java. 1.public final int getPriority(): java.lang.Thread.getPriority() method returns priority of given thread. 2.public final void setPriority(int newPriority): java.lang.Thread.setPriority() method changes the priority of thread to the value newPriority. This method throws IllegalArgumentException if value of parameter newPriority goes beyond minimum(1) and maximum(10) limit. • currentThread() method to get the name of the current thread. • setName() method to make names of thread as per choice for understanding purposes. • getName() method will be used to get the name of the thread.
  • 27. S Y N C H R O N I Z AT I O N I N J AVA • Synchronization in Java is the capability to control the access of multiple threads to any shared resource. • Java Synchronization is better option where we want to allow only one thread to access the shared resource. • The synchronization is mainly used to 1.To prevent thread interference. 2.To prevent consistency problem.
  • 28. T H R E A D S Y N C H R O N I Z AT I O N • There are two types of thread synchronization mutual exclusive and inter- thread communication. 1.Mutual Exclusive (no two processes can exist in the critical section at any given point of time) 2.Cooperation (Inter-thread communication in java)
  • 29. I N T E R -T H R E A D C O M M U N I C A T I O N : • Inter-thread communication is a process in which a thread is paused running in its critical region and another thread is allowed to enter (or lock) in the same critical region to be executed. i.e. synchronized threads communicate with each other.
  • 30. O B J E C T C L A S S M E T H O D S A R E U S E D F O R I N T E R - T H R E A D C O M M U N I C A T I O N P R O C E S S • wait(): this method instructs the current thread to release the monitor held by it and to get suspended until some other threads sends a notification from the same monitor. • Syntax: public void wait() throws InterruptedException. • notify(): this method is used to send the notification to the thread that is suspended by the wait() method. • Syntax: public void notify(). • notifyAll(): this method is used to send the notification to all the threads that are suspended by wait() method. • Syntax: public void notifyAll().
  • 31. P R O D U C E R - C O N S U M E R P R O B L E M T O U N D E R S T A N D I N T E R - T H R E A D C O M M U N I C A T I O N . • The producer-consumer problem (also known as the bounded-buffer problem) is a classic example of a multi-process synchronization problem. The problem describes two processes, the producer and the consumer, which share a common, fixed-size buffer used as a queue. • The producer’s job is to generate data, put it into the buffer, and start again. • At the same time, the consumer is consuming the data (i.e. removing it from the buffer), one piece at a time.
  • 32. • Problem To make sure that the producer won’t try to add data into the buffer if it’s full and that the consumer won’t try to remove data from an empty buffer.
  • 33. • Solution The producer is to either go to sleep or discard data if the buffer is full. The next time the consumer removes an item from the buffer, it notifies the producer, who starts to fill the buffer again. In the same way, the consumer can go to sleep if it finds the buffer to be empty. The next time the producer puts data into the buffer, it wakes up the sleeping consumer.