SlideShare a Scribd company logo
1 of 29
By
Harish
221348092
BCA ( AIML ) - B
TOPICINCLUDES:
 Introduction to Thread
 Creation of Thread
 Life cycle of Thread
 Stopping and Blocking a Thread
 Using Thread Methods
 Thread Priority
 Thread Synchronization
 DeadLock
INTRODUCTIONTO THREAD
• Process and Thread are two basic units of Java
program execution.
• Process: A process is a self contained execution
environment and it can be seen as a program or
application.
• Thread: It can be called lightweight process
• Thread requires less resources to create and exists in the
process
• Thread shares the process resources
INTRODUCTION
MULTITHREADING
• Multithreading in java is a process of
executing multiple processes simultaneously
• A program is divided into two or more
subprograms, which can be implemented at
the same time in parallel.
• Multiprocessing and multithreading, both are
used to achieve multitasking.
• Java Multithreading is mostly used in games,
animation etc.
MULTITHREADING
MULTITHREADING
ADVANTAGE:
 It doesn't block the user
 can perform many operations together so it
saves time.
 Threads are independent so it doesn't
affect other threads
CREATINGTHREAD
• Threads are implemented in the form of objects.
• The run() and start() are two inbuilt methods
which helps to thread implementation
• The run() method is the heart and soul of any
thread
– It makes up the entire body of a thread
• The run() method can be initiating with the help
of start() method.
CREATINGTHREAD
CREATING THREAD
1. By extending Thread class.
2. By implementing Runnable
interface.
CREATINGTHREAD
1. By Extending Thread class
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();
}
}
Output: thread is running…
// Extending thread class
// run() method declared
//object initiated
// run() method called through start()
CREATINGTHREAD
2. By implementing Runnable interface
 Define a class that implements Runnable
interface.
 The Runnable interface has only one method,
run(), that is to be defined in the method with the
code to be executed by the thread.
CREATINGTHREAD
2. By implementing Runnable interface
class Multi3 implements Runnable
{
public void run()
{
System.out.println("thread is running...");
}
public static void main(String args[])
{
Multi3 m1=new Multi3();
Thread t1 =new Thread(m1);
t1.start();
} }
// Implementing Runnable interface
// object initiated for class
// object initiated for thread
Output: thread is running…
LIFEcycleof a thread
• During the life time of a thread, there are
many states it can enter.
• They include:
1. Newborn state
2. Runnable state
3. Running state
4. Blocked state
5. Dead state
LIFEcycleof a thread
LIFEcycleof a thread
Newborn State:
 The thread is born and is said to be in newborn
state.
 The thread is not yet scheduled for running.
 At this state, we can do only one of the following:
• Schedule it for running using start() method.
• Kill it using stop() method.
LIFEcycleof a thread
Runnable State:
 The thread is ready for execution
 Waiting for the availability of the processor.
 The thread has joined the queue
LIFEcycleof a thread
Running State:
• Thread is executing
• The processor has given its time to the thread
for its execution.
• The thread runs until it gives up control on its
own or taken over by other threads.
LIFEcycleof a thread
Blocked State:
• A thread is said to be blocked
• It is prevented to entering into the runnable and the
running state.
• This happens when the thread is suspended, sleeping,
or waiting in order to satisfy certain requirements.
• A blocked thread is considered "not runnable" but not
dead and therefore fully qualified to run again.
• This state is achieved when we
Invoke suspend() or sleep() or wait() methods.
LIFEcycleof a thread
Dead State:
• Every thread has a life cycle.
• A running thread ends its life when it has completed
executing its run( ) method. It is a natural death.
• A thread can be killed in born, or in running, or even in
"not runnable" (blocked) condition.
• It is called premature death.
• This state is achieved when we invoke stop() method or
the thread completes it execution.
Threadmethods
• Thread is a class found in java.lang package.
Method Signature Description
String getName() Retrieves the name of running thread in the current
context in String format
void start()
This method will start a new thread of execution by
calling run() method of Thread/runnable object.
void run() This method is the entry point of the thread. Execution of
thread starts from this method.
void sleep(int sleeptime)
This method suspend the thread for mentioned time
duration in argument (sleeptime in ms)
void yield()
By invoking this method the current thread pause its
execution temporarily and allow other threads to
execute.
void join()
This method used to queue up a thread in execution.
Once called on thread, current thread will wait till calling
thread completes its execution
boolean isAlive() This method will check if thread is alive or dead
Stoppingandblocking
Stopping a thread:
• To stop a thread from running further, we may do
so by calling its stop() method.
• This causes a thread to stop immediately and
move it to its dead state.
• It forces the thread to stop abruptly before its
completion
• It causes premature death.
• To stop a thread we use the following syntax:
thread.stop();
Stoppingandblocking
Blocking a Thread:
• A thread can also be temporarily suspended
or blocked from entering into the runnable
and subsequently running state,
1. sleep(t) // blocked for ‘t’ milliseconds
2. suspend() // blocked until resume() method is invoked
3. wait() // blocked until notify () is invoked
Threadpriority
• Each thread is assigned a priority, which
affects the order in which it is scheduled for
running.
• Java permits us to set the priority of a thread
using the setPriority() method as follows:
ThreadName.setPriority(int Number);
Threadpriority
• The intNumber is an integer value to which the
thread's priority is set. The Thread class defines
several priority constants:
1. public static int MIN_PRIORITY = 1
2. public static int NORM_PRIORITY = 5
3. public static int MAX_PRIORITY = 10
• The default setting is NORM_PRIORITY. Most user-
level processes should use NORM_PRIORITY.
Javasynchronization
• Generally threads use their own data and
methods provided inside their run() methods.
• But if we wish to use data and methods outside
the thread’s run() method, they may compete
for the same resources and may lead to serious
problems.
• Java enables us to overcome this problem using
a technique known as Synchronization.
For ex.: One thread may try to read a record from
a file while another is still writing to the same file.
Javasynchronization
• When the method declared as synchronized,
Java creates a "monitor" and hands it over to
the thread that calls the method first time.
synchronized (lock-object)
{
.......... // code here is synchronized
}
deadlock
• Deadlock describes a situation where two or
more threads are blocked forever, waiting for
each other.
• when two or more threads are waiting to gain
control on a resource.
For example, assume that the thread A must
access Method1 before it can release Method2, but
the thread B cannot release Method1 until it gets
holds of Method2.
deadlock
THANK YOU…

More Related Content

Similar to Multithreadingppt.pptx

Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaMonika Mishra
 
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 in Java Object Oriented Programming language
Multithreading in Java Object Oriented Programming languageMultithreading in Java Object Oriented Programming language
Multithreading in Java Object Oriented Programming languagearnavytstudio2814
 
Multithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of threadMultithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of threadKartik Dube
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaArafat Hossan
 
Multi threading
Multi threadingMulti threading
Multi threadinggndu
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javajunnubabu
 
Multithreading programming in java
Multithreading programming in javaMultithreading programming in java
Multithreading programming in javaElizabeth alexander
 
Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...
Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...
Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...Sachintha Gunasena
 
Basic of Multithreading in JAva
Basic of Multithreading in JAvaBasic of Multithreading in JAva
Basic of Multithreading in JAvasuraj pandey
 

Similar to Multithreadingppt.pptx (20)

Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
 
Threading concepts
Threading conceptsThreading concepts
Threading concepts
 
Threads in Java
Threads in JavaThreads in Java
Threads in Java
 
Unit-3 MULTITHREADING-2.pdf
Unit-3 MULTITHREADING-2.pdfUnit-3 MULTITHREADING-2.pdf
Unit-3 MULTITHREADING-2.pdf
 
Java unit 12
Java unit 12Java unit 12
Java unit 12
 
Multithreading in Java Object Oriented Programming language
Multithreading in Java Object Oriented Programming languageMultithreading in Java Object Oriented Programming language
Multithreading in Java Object Oriented Programming language
 
Multithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of threadMultithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of thread
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Java thread life cycle
Java thread life cycleJava thread life cycle
Java thread life cycle
 
Multi threading
Multi threadingMulti threading
Multi threading
 
Md09 multithreading
Md09 multithreadingMd09 multithreading
Md09 multithreading
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
multithreading.pptx
multithreading.pptxmultithreading.pptx
multithreading.pptx
 
Multithreading programming in java
Multithreading programming in javaMultithreading programming in java
Multithreading programming in java
 
Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...
Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...
Concurrency Programming in Java - 05 - Processes and Threads, Thread Objects,...
 
7. Multithreading
7. Multithreading7. Multithreading
7. Multithreading
 
Basic of Multithreading in JAva
Basic of Multithreading in JAvaBasic of Multithreading in JAva
Basic of Multithreading in JAva
 
Multi threading
Multi threadingMulti threading
Multi threading
 

More from HKShab

java 4kfehsd rfednjskm, kwrefndkws efhdjks fjds.pptx
java 4kfehsd rfednjskm, kwrefndkws efhdjks fjds.pptxjava 4kfehsd rfednjskm, kwrefndkws efhdjks fjds.pptx
java 4kfehsd rfednjskm, kwrefndkws efhdjks fjds.pptxHKShab
 
Virtual Memory sjkdhikejv vsdkjnksnv vkjhfvk
Virtual Memory sjkdhikejv vsdkjnksnv vkjhfvkVirtual Memory sjkdhikejv vsdkjnksnv vkjhfvk
Virtual Memory sjkdhikejv vsdkjnksnv vkjhfvkHKShab
 
Unit-1 part 2.pptx
Unit-1 part 2.pptxUnit-1 part 2.pptx
Unit-1 part 2.pptxHKShab
 
Pharmacymanagement- Harish(221348092).pptx
Pharmacymanagement- Harish(221348092).pptxPharmacymanagement- Harish(221348092).pptx
Pharmacymanagement- Harish(221348092).pptxHKShab
 
assamppt-161024172823.pptx
assamppt-161024172823.pptxassamppt-161024172823.pptx
assamppt-161024172823.pptxHKShab
 
1's and 2's complement.pptx
1's and 2's complement.pptx1's and 2's complement.pptx
1's and 2's complement.pptxHKShab
 
1sand2scomplement.pptx
1sand2scomplement.pptx1sand2scomplement.pptx
1sand2scomplement.pptxHKShab
 
hp.pptx
hp.pptxhp.pptx
hp.pptxHKShab
 
Ankit (221348051) BCA-Aiml.pptx
Ankit (221348051) BCA-Aiml.pptxAnkit (221348051) BCA-Aiml.pptx
Ankit (221348051) BCA-Aiml.pptxHKShab
 
Ram Singh (221348062) - Cloud Computing.pptx
Ram Singh (221348062) - Cloud Computing.pptxRam Singh (221348062) - Cloud Computing.pptx
Ram Singh (221348062) - Cloud Computing.pptxHKShab
 
Sahil Presentation1.pptx
Sahil Presentation1.pptxSahil Presentation1.pptx
Sahil Presentation1.pptxHKShab
 

More from HKShab (11)

java 4kfehsd rfednjskm, kwrefndkws efhdjks fjds.pptx
java 4kfehsd rfednjskm, kwrefndkws efhdjks fjds.pptxjava 4kfehsd rfednjskm, kwrefndkws efhdjks fjds.pptx
java 4kfehsd rfednjskm, kwrefndkws efhdjks fjds.pptx
 
Virtual Memory sjkdhikejv vsdkjnksnv vkjhfvk
Virtual Memory sjkdhikejv vsdkjnksnv vkjhfvkVirtual Memory sjkdhikejv vsdkjnksnv vkjhfvk
Virtual Memory sjkdhikejv vsdkjnksnv vkjhfvk
 
Unit-1 part 2.pptx
Unit-1 part 2.pptxUnit-1 part 2.pptx
Unit-1 part 2.pptx
 
Pharmacymanagement- Harish(221348092).pptx
Pharmacymanagement- Harish(221348092).pptxPharmacymanagement- Harish(221348092).pptx
Pharmacymanagement- Harish(221348092).pptx
 
assamppt-161024172823.pptx
assamppt-161024172823.pptxassamppt-161024172823.pptx
assamppt-161024172823.pptx
 
1's and 2's complement.pptx
1's and 2's complement.pptx1's and 2's complement.pptx
1's and 2's complement.pptx
 
1sand2scomplement.pptx
1sand2scomplement.pptx1sand2scomplement.pptx
1sand2scomplement.pptx
 
hp.pptx
hp.pptxhp.pptx
hp.pptx
 
Ankit (221348051) BCA-Aiml.pptx
Ankit (221348051) BCA-Aiml.pptxAnkit (221348051) BCA-Aiml.pptx
Ankit (221348051) BCA-Aiml.pptx
 
Ram Singh (221348062) - Cloud Computing.pptx
Ram Singh (221348062) - Cloud Computing.pptxRam Singh (221348062) - Cloud Computing.pptx
Ram Singh (221348062) - Cloud Computing.pptx
 
Sahil Presentation1.pptx
Sahil Presentation1.pptxSahil Presentation1.pptx
Sahil Presentation1.pptx
 

Recently uploaded

Hotel And Home Service Available Kolkata Call Girls South End Park ✔ 62971435...
Hotel And Home Service Available Kolkata Call Girls South End Park ✔ 62971435...Hotel And Home Service Available Kolkata Call Girls South End Park ✔ 62971435...
Hotel And Home Service Available Kolkata Call Girls South End Park ✔ 62971435...ritikasharma
 
📞 Contact Number 8617697112 VIP Ganderbal Call Girls
📞 Contact Number 8617697112 VIP Ganderbal Call Girls📞 Contact Number 8617697112 VIP Ganderbal Call Girls
📞 Contact Number 8617697112 VIP Ganderbal Call GirlsNitya salvi
 
Zirakpur Call Girls👧 Book Now📱8146719683 📞👉Mohali Call Girl Service No Advanc...
Zirakpur Call Girls👧 Book Now📱8146719683 📞👉Mohali Call Girl Service No Advanc...Zirakpur Call Girls👧 Book Now📱8146719683 📞👉Mohali Call Girl Service No Advanc...
Zirakpur Call Girls👧 Book Now📱8146719683 📞👉Mohali Call Girl Service No Advanc...rajveermohali2022
 
Top Rated Kolkata Call Girls Khardah ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...
Top Rated Kolkata Call Girls Khardah ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...Top Rated Kolkata Call Girls Khardah ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...
Top Rated Kolkata Call Girls Khardah ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...ritikasharma
 
Hotel And Home Service Available Kolkata Call Girls Diamond Harbour ✔ 6297143...
Hotel And Home Service Available Kolkata Call Girls Diamond Harbour ✔ 6297143...Hotel And Home Service Available Kolkata Call Girls Diamond Harbour ✔ 6297143...
Hotel And Home Service Available Kolkata Call Girls Diamond Harbour ✔ 6297143...ritikasharma
 
𓀤Call On 6297143586 𓀤 Ultadanga Call Girls In All Kolkata 24/7 Provide Call W...
𓀤Call On 6297143586 𓀤 Ultadanga Call Girls In All Kolkata 24/7 Provide Call W...𓀤Call On 6297143586 𓀤 Ultadanga Call Girls In All Kolkata 24/7 Provide Call W...
𓀤Call On 6297143586 𓀤 Ultadanga Call Girls In All Kolkata 24/7 Provide Call W...rahim quresi
 
Book Sex Workers Available Kolkata Call Girls Service Airport Kolkata ✔ 62971...
Book Sex Workers Available Kolkata Call Girls Service Airport Kolkata ✔ 62971...Book Sex Workers Available Kolkata Call Girls Service Airport Kolkata ✔ 62971...
Book Sex Workers Available Kolkata Call Girls Service Airport Kolkata ✔ 62971...ritikasharma
 
Verified Trusted Call Girls Singaperumal Koil Chennai ✔✔7427069034 Independe...
Verified Trusted Call Girls Singaperumal Koil Chennai ✔✔7427069034  Independe...Verified Trusted Call Girls Singaperumal Koil Chennai ✔✔7427069034  Independe...
Verified Trusted Call Girls Singaperumal Koil Chennai ✔✔7427069034 Independe... Shivani Pandey
 
Kanpur call girls 📞 8617697112 At Low Cost Cash Payment Booking
Kanpur call girls 📞 8617697112 At Low Cost Cash Payment BookingKanpur call girls 📞 8617697112 At Low Cost Cash Payment Booking
Kanpur call girls 📞 8617697112 At Low Cost Cash Payment BookingNitya salvi
 
Call Girls Bhandara Just Call 8617697112 Top Class Call Girl Service Available
Call Girls Bhandara Just Call 8617697112 Top Class Call Girl Service AvailableCall Girls Bhandara Just Call 8617697112 Top Class Call Girl Service Available
Call Girls Bhandara Just Call 8617697112 Top Class Call Girl Service AvailableNitya salvi
 
𓀤Call On 6297143586 𓀤 Park Street Call Girls In All Kolkata 24/7 Provide Call...
𓀤Call On 6297143586 𓀤 Park Street Call Girls In All Kolkata 24/7 Provide Call...𓀤Call On 6297143586 𓀤 Park Street Call Girls In All Kolkata 24/7 Provide Call...
𓀤Call On 6297143586 𓀤 Park Street Call Girls In All Kolkata 24/7 Provide Call...rahim quresi
 
(TOP CLASS) Call Girls In Chengalpattu Phone 7427069034 Call Girls Model With...
(TOP CLASS) Call Girls In Chengalpattu Phone 7427069034 Call Girls Model With...(TOP CLASS) Call Girls In Chengalpattu Phone 7427069034 Call Girls Model With...
(TOP CLASS) Call Girls In Chengalpattu Phone 7427069034 Call Girls Model With... Shivani Pandey
 
Hotel And Home Service Available Kolkata Call Girls Lake Town ✔ 6297143586 ✔C...
Hotel And Home Service Available Kolkata Call Girls Lake Town ✔ 6297143586 ✔C...Hotel And Home Service Available Kolkata Call Girls Lake Town ✔ 6297143586 ✔C...
Hotel And Home Service Available Kolkata Call Girls Lake Town ✔ 6297143586 ✔C...ritikasharma
 
Almora call girls 📞 8617697112 At Low Cost Cash Payment Booking
Almora call girls 📞 8617697112 At Low Cost Cash Payment BookingAlmora call girls 📞 8617697112 At Low Cost Cash Payment Booking
Almora call girls 📞 8617697112 At Low Cost Cash Payment BookingNitya salvi
 
Behala ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Ready ...
Behala ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Ready ...Behala ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Ready ...
Behala ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Ready ...ritikasharma
 
Sonagachi ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...
Sonagachi ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...Sonagachi ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...
Sonagachi ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...rahim quresi
 
📞 Contact Number 8617697112 VIP East Sikkim Call Girls
📞 Contact Number 8617697112 VIP East Sikkim Call Girls📞 Contact Number 8617697112 VIP East Sikkim Call Girls
📞 Contact Number 8617697112 VIP East Sikkim Call GirlsNitya salvi
 
Thane West \ Escort Service in Mumbai - 450+ Call Girl Cash Payment 983332523...
Thane West \ Escort Service in Mumbai - 450+ Call Girl Cash Payment 983332523...Thane West \ Escort Service in Mumbai - 450+ Call Girl Cash Payment 983332523...
Thane West \ Escort Service in Mumbai - 450+ Call Girl Cash Payment 983332523...hotbabesbook
 
Top Rated Pune Call Girls Pimpri Chinchwad ⟟ 6297143586 ⟟ Call Me For Genuin...
Top Rated  Pune Call Girls Pimpri Chinchwad ⟟ 6297143586 ⟟ Call Me For Genuin...Top Rated  Pune Call Girls Pimpri Chinchwad ⟟ 6297143586 ⟟ Call Me For Genuin...
Top Rated Pune Call Girls Pimpri Chinchwad ⟟ 6297143586 ⟟ Call Me For Genuin...Call Girls in Nagpur High Profile
 
Hotel And Home Service Available Kolkata Call Girls Park Street ✔ 6297143586 ...
Hotel And Home Service Available Kolkata Call Girls Park Street ✔ 6297143586 ...Hotel And Home Service Available Kolkata Call Girls Park Street ✔ 6297143586 ...
Hotel And Home Service Available Kolkata Call Girls Park Street ✔ 6297143586 ...ritikasharma
 

Recently uploaded (20)

Hotel And Home Service Available Kolkata Call Girls South End Park ✔ 62971435...
Hotel And Home Service Available Kolkata Call Girls South End Park ✔ 62971435...Hotel And Home Service Available Kolkata Call Girls South End Park ✔ 62971435...
Hotel And Home Service Available Kolkata Call Girls South End Park ✔ 62971435...
 
📞 Contact Number 8617697112 VIP Ganderbal Call Girls
📞 Contact Number 8617697112 VIP Ganderbal Call Girls📞 Contact Number 8617697112 VIP Ganderbal Call Girls
📞 Contact Number 8617697112 VIP Ganderbal Call Girls
 
Zirakpur Call Girls👧 Book Now📱8146719683 📞👉Mohali Call Girl Service No Advanc...
Zirakpur Call Girls👧 Book Now📱8146719683 📞👉Mohali Call Girl Service No Advanc...Zirakpur Call Girls👧 Book Now📱8146719683 📞👉Mohali Call Girl Service No Advanc...
Zirakpur Call Girls👧 Book Now📱8146719683 📞👉Mohali Call Girl Service No Advanc...
 
Top Rated Kolkata Call Girls Khardah ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...
Top Rated Kolkata Call Girls Khardah ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...Top Rated Kolkata Call Girls Khardah ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...
Top Rated Kolkata Call Girls Khardah ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...
 
Hotel And Home Service Available Kolkata Call Girls Diamond Harbour ✔ 6297143...
Hotel And Home Service Available Kolkata Call Girls Diamond Harbour ✔ 6297143...Hotel And Home Service Available Kolkata Call Girls Diamond Harbour ✔ 6297143...
Hotel And Home Service Available Kolkata Call Girls Diamond Harbour ✔ 6297143...
 
𓀤Call On 6297143586 𓀤 Ultadanga Call Girls In All Kolkata 24/7 Provide Call W...
𓀤Call On 6297143586 𓀤 Ultadanga Call Girls In All Kolkata 24/7 Provide Call W...𓀤Call On 6297143586 𓀤 Ultadanga Call Girls In All Kolkata 24/7 Provide Call W...
𓀤Call On 6297143586 𓀤 Ultadanga Call Girls In All Kolkata 24/7 Provide Call W...
 
Book Sex Workers Available Kolkata Call Girls Service Airport Kolkata ✔ 62971...
Book Sex Workers Available Kolkata Call Girls Service Airport Kolkata ✔ 62971...Book Sex Workers Available Kolkata Call Girls Service Airport Kolkata ✔ 62971...
Book Sex Workers Available Kolkata Call Girls Service Airport Kolkata ✔ 62971...
 
Verified Trusted Call Girls Singaperumal Koil Chennai ✔✔7427069034 Independe...
Verified Trusted Call Girls Singaperumal Koil Chennai ✔✔7427069034  Independe...Verified Trusted Call Girls Singaperumal Koil Chennai ✔✔7427069034  Independe...
Verified Trusted Call Girls Singaperumal Koil Chennai ✔✔7427069034 Independe...
 
Kanpur call girls 📞 8617697112 At Low Cost Cash Payment Booking
Kanpur call girls 📞 8617697112 At Low Cost Cash Payment BookingKanpur call girls 📞 8617697112 At Low Cost Cash Payment Booking
Kanpur call girls 📞 8617697112 At Low Cost Cash Payment Booking
 
Call Girls Bhandara Just Call 8617697112 Top Class Call Girl Service Available
Call Girls Bhandara Just Call 8617697112 Top Class Call Girl Service AvailableCall Girls Bhandara Just Call 8617697112 Top Class Call Girl Service Available
Call Girls Bhandara Just Call 8617697112 Top Class Call Girl Service Available
 
𓀤Call On 6297143586 𓀤 Park Street Call Girls In All Kolkata 24/7 Provide Call...
𓀤Call On 6297143586 𓀤 Park Street Call Girls In All Kolkata 24/7 Provide Call...𓀤Call On 6297143586 𓀤 Park Street Call Girls In All Kolkata 24/7 Provide Call...
𓀤Call On 6297143586 𓀤 Park Street Call Girls In All Kolkata 24/7 Provide Call...
 
(TOP CLASS) Call Girls In Chengalpattu Phone 7427069034 Call Girls Model With...
(TOP CLASS) Call Girls In Chengalpattu Phone 7427069034 Call Girls Model With...(TOP CLASS) Call Girls In Chengalpattu Phone 7427069034 Call Girls Model With...
(TOP CLASS) Call Girls In Chengalpattu Phone 7427069034 Call Girls Model With...
 
Hotel And Home Service Available Kolkata Call Girls Lake Town ✔ 6297143586 ✔C...
Hotel And Home Service Available Kolkata Call Girls Lake Town ✔ 6297143586 ✔C...Hotel And Home Service Available Kolkata Call Girls Lake Town ✔ 6297143586 ✔C...
Hotel And Home Service Available Kolkata Call Girls Lake Town ✔ 6297143586 ✔C...
 
Almora call girls 📞 8617697112 At Low Cost Cash Payment Booking
Almora call girls 📞 8617697112 At Low Cost Cash Payment BookingAlmora call girls 📞 8617697112 At Low Cost Cash Payment Booking
Almora call girls 📞 8617697112 At Low Cost Cash Payment Booking
 
Behala ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Ready ...
Behala ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Ready ...Behala ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Ready ...
Behala ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Ready ...
 
Sonagachi ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...
Sonagachi ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...Sonagachi ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...
Sonagachi ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Rea...
 
📞 Contact Number 8617697112 VIP East Sikkim Call Girls
📞 Contact Number 8617697112 VIP East Sikkim Call Girls📞 Contact Number 8617697112 VIP East Sikkim Call Girls
📞 Contact Number 8617697112 VIP East Sikkim Call Girls
 
Thane West \ Escort Service in Mumbai - 450+ Call Girl Cash Payment 983332523...
Thane West \ Escort Service in Mumbai - 450+ Call Girl Cash Payment 983332523...Thane West \ Escort Service in Mumbai - 450+ Call Girl Cash Payment 983332523...
Thane West \ Escort Service in Mumbai - 450+ Call Girl Cash Payment 983332523...
 
Top Rated Pune Call Girls Pimpri Chinchwad ⟟ 6297143586 ⟟ Call Me For Genuin...
Top Rated  Pune Call Girls Pimpri Chinchwad ⟟ 6297143586 ⟟ Call Me For Genuin...Top Rated  Pune Call Girls Pimpri Chinchwad ⟟ 6297143586 ⟟ Call Me For Genuin...
Top Rated Pune Call Girls Pimpri Chinchwad ⟟ 6297143586 ⟟ Call Me For Genuin...
 
Hotel And Home Service Available Kolkata Call Girls Park Street ✔ 6297143586 ...
Hotel And Home Service Available Kolkata Call Girls Park Street ✔ 6297143586 ...Hotel And Home Service Available Kolkata Call Girls Park Street ✔ 6297143586 ...
Hotel And Home Service Available Kolkata Call Girls Park Street ✔ 6297143586 ...
 

Multithreadingppt.pptx

  • 2. TOPICINCLUDES:  Introduction to Thread  Creation of Thread  Life cycle of Thread  Stopping and Blocking a Thread  Using Thread Methods  Thread Priority  Thread Synchronization  DeadLock
  • 3. INTRODUCTIONTO THREAD • Process and Thread are two basic units of Java program execution. • Process: A process is a self contained execution environment and it can be seen as a program or application. • Thread: It can be called lightweight process • Thread requires less resources to create and exists in the process • Thread shares the process resources
  • 5. MULTITHREADING • Multithreading in java is a process of executing multiple processes simultaneously • A program is divided into two or more subprograms, which can be implemented at the same time in parallel. • Multiprocessing and multithreading, both are used to achieve multitasking. • Java Multithreading is mostly used in games, animation etc.
  • 7. MULTITHREADING ADVANTAGE:  It doesn't block the user  can perform many operations together so it saves time.  Threads are independent so it doesn't affect other threads
  • 8. CREATINGTHREAD • Threads are implemented in the form of objects. • The run() and start() are two inbuilt methods which helps to thread implementation • The run() method is the heart and soul of any thread – It makes up the entire body of a thread • The run() method can be initiating with the help of start() method.
  • 9. CREATINGTHREAD CREATING THREAD 1. By extending Thread class. 2. By implementing Runnable interface.
  • 10. CREATINGTHREAD 1. By Extending Thread class 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(); } } Output: thread is running… // Extending thread class // run() method declared //object initiated // run() method called through start()
  • 11. CREATINGTHREAD 2. By implementing Runnable interface  Define a class that implements Runnable interface.  The Runnable interface has only one method, run(), that is to be defined in the method with the code to be executed by the thread.
  • 12. CREATINGTHREAD 2. By implementing Runnable interface class Multi3 implements Runnable { public void run() { System.out.println("thread is running..."); } public static void main(String args[]) { Multi3 m1=new Multi3(); Thread t1 =new Thread(m1); t1.start(); } } // Implementing Runnable interface // object initiated for class // object initiated for thread Output: thread is running…
  • 13. LIFEcycleof a thread • During the life time of a thread, there are many states it can enter. • They include: 1. Newborn state 2. Runnable state 3. Running state 4. Blocked state 5. Dead state
  • 15. LIFEcycleof a thread Newborn State:  The thread is born and is said to be in newborn state.  The thread is not yet scheduled for running.  At this state, we can do only one of the following: • Schedule it for running using start() method. • Kill it using stop() method.
  • 16. LIFEcycleof a thread Runnable State:  The thread is ready for execution  Waiting for the availability of the processor.  The thread has joined the queue
  • 17. LIFEcycleof a thread Running State: • Thread is executing • The processor has given its time to the thread for its execution. • The thread runs until it gives up control on its own or taken over by other threads.
  • 18. LIFEcycleof a thread Blocked State: • A thread is said to be blocked • It is prevented to entering into the runnable and the running state. • This happens when the thread is suspended, sleeping, or waiting in order to satisfy certain requirements. • A blocked thread is considered "not runnable" but not dead and therefore fully qualified to run again. • This state is achieved when we Invoke suspend() or sleep() or wait() methods.
  • 19. LIFEcycleof a thread Dead State: • Every thread has a life cycle. • A running thread ends its life when it has completed executing its run( ) method. It is a natural death. • A thread can be killed in born, or in running, or even in "not runnable" (blocked) condition. • It is called premature death. • This state is achieved when we invoke stop() method or the thread completes it execution.
  • 20. Threadmethods • Thread is a class found in java.lang package. Method Signature Description String getName() Retrieves the name of running thread in the current context in String format void start() This method will start a new thread of execution by calling run() method of Thread/runnable object. void run() This method is the entry point of the thread. Execution of thread starts from this method. void sleep(int sleeptime) This method suspend the thread for mentioned time duration in argument (sleeptime in ms) void yield() By invoking this method the current thread pause its execution temporarily and allow other threads to execute. void join() This method used to queue up a thread in execution. Once called on thread, current thread will wait till calling thread completes its execution boolean isAlive() This method will check if thread is alive or dead
  • 21. Stoppingandblocking Stopping a thread: • To stop a thread from running further, we may do so by calling its stop() method. • This causes a thread to stop immediately and move it to its dead state. • It forces the thread to stop abruptly before its completion • It causes premature death. • To stop a thread we use the following syntax: thread.stop();
  • 22. Stoppingandblocking Blocking a Thread: • A thread can also be temporarily suspended or blocked from entering into the runnable and subsequently running state, 1. sleep(t) // blocked for ‘t’ milliseconds 2. suspend() // blocked until resume() method is invoked 3. wait() // blocked until notify () is invoked
  • 23. Threadpriority • Each thread is assigned a priority, which affects the order in which it is scheduled for running. • Java permits us to set the priority of a thread using the setPriority() method as follows: ThreadName.setPriority(int Number);
  • 24. Threadpriority • The intNumber is an integer value to which the thread's priority is set. The Thread class defines several priority constants: 1. public static int MIN_PRIORITY = 1 2. public static int NORM_PRIORITY = 5 3. public static int MAX_PRIORITY = 10 • The default setting is NORM_PRIORITY. Most user- level processes should use NORM_PRIORITY.
  • 25. Javasynchronization • Generally threads use their own data and methods provided inside their run() methods. • But if we wish to use data and methods outside the thread’s run() method, they may compete for the same resources and may lead to serious problems. • Java enables us to overcome this problem using a technique known as Synchronization. For ex.: One thread may try to read a record from a file while another is still writing to the same file.
  • 26. Javasynchronization • When the method declared as synchronized, Java creates a "monitor" and hands it over to the thread that calls the method first time. synchronized (lock-object) { .......... // code here is synchronized }
  • 27. deadlock • Deadlock describes a situation where two or more threads are blocked forever, waiting for each other. • when two or more threads are waiting to gain control on a resource. For example, assume that the thread A must access Method1 before it can release Method2, but the thread B cannot release Method1 until it gets holds of Method2.