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

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 (10)

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

Air-Hostess Call Girls Shobhabazar | 8250192130 At Low Cost Cash Payment Booking
Air-Hostess Call Girls Shobhabazar | 8250192130 At Low Cost Cash Payment BookingAir-Hostess Call Girls Shobhabazar | 8250192130 At Low Cost Cash Payment Booking
Air-Hostess Call Girls Shobhabazar | 8250192130 At Low Cost Cash Payment BookingRiya Pathan
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377087607
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377087607FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377087607
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377087607dollysharma2066
 
Verified Call Girls Esplanade - [ Cash on Delivery ] Contact 8250192130 Escor...
Verified Call Girls Esplanade - [ Cash on Delivery ] Contact 8250192130 Escor...Verified Call Girls Esplanade - [ Cash on Delivery ] Contact 8250192130 Escor...
Verified Call Girls Esplanade - [ Cash on Delivery ] Contact 8250192130 Escor...anamikaraghav4
 
5* Hotel Call Girls In Goa 7028418221 Call Girls In North Goa Escort Services
5* Hotel Call Girls In Goa 7028418221 Call Girls In North Goa Escort Services5* Hotel Call Girls In Goa 7028418221 Call Girls In North Goa Escort Services
5* Hotel Call Girls In Goa 7028418221 Call Girls In North Goa Escort ServicesApsara Of India
 
ViP Call Girls In Udaipur 9602870969 Gulab Bagh Escorts SeRvIcE
ViP Call Girls In Udaipur 9602870969 Gulab Bagh Escorts SeRvIcEViP Call Girls In Udaipur 9602870969 Gulab Bagh Escorts SeRvIcE
ViP Call Girls In Udaipur 9602870969 Gulab Bagh Escorts SeRvIcEApsara Of India
 
VIP Call Girls Bara Bazar - Phone No 8250192130 For Ultimate Sexual Urges
VIP Call Girls Bara Bazar - Phone No 8250192130 For Ultimate Sexual UrgesVIP Call Girls Bara Bazar - Phone No 8250192130 For Ultimate Sexual Urges
VIP Call Girls Bara Bazar - Phone No 8250192130 For Ultimate Sexual Urgesanamikaraghav4
 
Call Girl Nashik Saloni 7001305949 Independent Escort Service Nashik
Call Girl Nashik Saloni 7001305949 Independent Escort Service NashikCall Girl Nashik Saloni 7001305949 Independent Escort Service Nashik
Call Girl Nashik Saloni 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
VIP Call Girl Kolhapur Aashi 8250192130 Independent Escort Service Kolhapur
VIP Call Girl Kolhapur Aashi 8250192130 Independent Escort Service KolhapurVIP Call Girl Kolhapur Aashi 8250192130 Independent Escort Service Kolhapur
VIP Call Girl Kolhapur Aashi 8250192130 Independent Escort Service KolhapurRiya Pathan
 
Cash Payment Contact:- 7028418221 Goa Call Girls Service North Goa Escorts
Cash Payment Contact:- 7028418221 Goa Call Girls Service North Goa EscortsCash Payment Contact:- 7028418221 Goa Call Girls Service North Goa Escorts
Cash Payment Contact:- 7028418221 Goa Call Girls Service North Goa EscortsApsara Of India
 
Kolkata Call Girl Bara Bazar 👉 8250192130 ❣️💯 Available With Room 24×7
Kolkata Call Girl Bara Bazar 👉 8250192130 ❣️💯 Available With Room 24×7Kolkata Call Girl Bara Bazar 👉 8250192130 ❣️💯 Available With Room 24×7
Kolkata Call Girl Bara Bazar 👉 8250192130 ❣️💯 Available With Room 24×7Riya Pathan
 
VIP Call Girls In Goa 7028418221 Call Girls In Baga Beach Escorts Service
VIP Call Girls In Goa 7028418221 Call Girls In Baga Beach Escorts ServiceVIP Call Girls In Goa 7028418221 Call Girls In Baga Beach Escorts Service
VIP Call Girls In Goa 7028418221 Call Girls In Baga Beach Escorts ServiceApsara Of India
 
Kolkata Call Girl Airport Kolkata 👉 8250192130 ❣️💯 Available With Room 24×7
Kolkata Call Girl Airport Kolkata 👉 8250192130 ❣️💯 Available With Room 24×7Kolkata Call Girl Airport Kolkata 👉 8250192130 ❣️💯 Available With Room 24×7
Kolkata Call Girl Airport Kolkata 👉 8250192130 ❣️💯 Available With Room 24×7Riya Pathan
 
Call Girls Somajiguda Sarani 7001305949 all area service COD available Any Time
Call Girls Somajiguda Sarani 7001305949 all area service COD available Any TimeCall Girls Somajiguda Sarani 7001305949 all area service COD available Any Time
Call Girls Somajiguda Sarani 7001305949 all area service COD available Any Timedelhimodelshub1
 
Beyond Bar & Club Udaipur CaLL GiRLS 09602870969
Beyond Bar & Club Udaipur CaLL GiRLS 09602870969Beyond Bar & Club Udaipur CaLL GiRLS 09602870969
Beyond Bar & Club Udaipur CaLL GiRLS 09602870969Apsara Of India
 
Call Girls Nashik Gayatri 7001305949 Independent Escort Service Nashik
Call Girls Nashik Gayatri 7001305949 Independent Escort Service NashikCall Girls Nashik Gayatri 7001305949 Independent Escort Service Nashik
Call Girls Nashik Gayatri 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Kolkata Call Girl Howrah 👉 8250192130 ❣️💯 Available With Room 24×7
Kolkata Call Girl Howrah 👉 8250192130 ❣️💯 Available With Room 24×7Kolkata Call Girl Howrah 👉 8250192130 ❣️💯 Available With Room 24×7
Kolkata Call Girl Howrah 👉 8250192130 ❣️💯 Available With Room 24×7Riya Pathan
 
(Dipika) Call Girls in Bangur ! 8250192130 ₹2999 Only and Free Hotel Delivery...
(Dipika) Call Girls in Bangur ! 8250192130 ₹2999 Only and Free Hotel Delivery...(Dipika) Call Girls in Bangur ! 8250192130 ₹2999 Only and Free Hotel Delivery...
(Dipika) Call Girls in Bangur ! 8250192130 ₹2999 Only and Free Hotel Delivery...Riya Pathan
 
(DIVYA) Dhanori Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(DIVYA) Dhanori Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(DIVYA) Dhanori Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(DIVYA) Dhanori Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...ranjana rawat
 
(KRITI) Pimpri Chinchwad Call Girls Just Call 7001035870 [ Cash on Delivery ]...
(KRITI) Pimpri Chinchwad Call Girls Just Call 7001035870 [ Cash on Delivery ]...(KRITI) Pimpri Chinchwad Call Girls Just Call 7001035870 [ Cash on Delivery ]...
(KRITI) Pimpri Chinchwad Call Girls Just Call 7001035870 [ Cash on Delivery ]...ranjana rawat
 

Recently uploaded (20)

Air-Hostess Call Girls Shobhabazar | 8250192130 At Low Cost Cash Payment Booking
Air-Hostess Call Girls Shobhabazar | 8250192130 At Low Cost Cash Payment BookingAir-Hostess Call Girls Shobhabazar | 8250192130 At Low Cost Cash Payment Booking
Air-Hostess Call Girls Shobhabazar | 8250192130 At Low Cost Cash Payment Booking
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377087607
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377087607FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377087607
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377087607
 
Verified Call Girls Esplanade - [ Cash on Delivery ] Contact 8250192130 Escor...
Verified Call Girls Esplanade - [ Cash on Delivery ] Contact 8250192130 Escor...Verified Call Girls Esplanade - [ Cash on Delivery ] Contact 8250192130 Escor...
Verified Call Girls Esplanade - [ Cash on Delivery ] Contact 8250192130 Escor...
 
5* Hotel Call Girls In Goa 7028418221 Call Girls In North Goa Escort Services
5* Hotel Call Girls In Goa 7028418221 Call Girls In North Goa Escort Services5* Hotel Call Girls In Goa 7028418221 Call Girls In North Goa Escort Services
5* Hotel Call Girls In Goa 7028418221 Call Girls In North Goa Escort Services
 
ViP Call Girls In Udaipur 9602870969 Gulab Bagh Escorts SeRvIcE
ViP Call Girls In Udaipur 9602870969 Gulab Bagh Escorts SeRvIcEViP Call Girls In Udaipur 9602870969 Gulab Bagh Escorts SeRvIcE
ViP Call Girls In Udaipur 9602870969 Gulab Bagh Escorts SeRvIcE
 
VIP Call Girls Bara Bazar - Phone No 8250192130 For Ultimate Sexual Urges
VIP Call Girls Bara Bazar - Phone No 8250192130 For Ultimate Sexual UrgesVIP Call Girls Bara Bazar - Phone No 8250192130 For Ultimate Sexual Urges
VIP Call Girls Bara Bazar - Phone No 8250192130 For Ultimate Sexual Urges
 
Call Girl Nashik Saloni 7001305949 Independent Escort Service Nashik
Call Girl Nashik Saloni 7001305949 Independent Escort Service NashikCall Girl Nashik Saloni 7001305949 Independent Escort Service Nashik
Call Girl Nashik Saloni 7001305949 Independent Escort Service Nashik
 
VIP Call Girl Kolhapur Aashi 8250192130 Independent Escort Service Kolhapur
VIP Call Girl Kolhapur Aashi 8250192130 Independent Escort Service KolhapurVIP Call Girl Kolhapur Aashi 8250192130 Independent Escort Service Kolhapur
VIP Call Girl Kolhapur Aashi 8250192130 Independent Escort Service Kolhapur
 
Cash Payment Contact:- 7028418221 Goa Call Girls Service North Goa Escorts
Cash Payment Contact:- 7028418221 Goa Call Girls Service North Goa EscortsCash Payment Contact:- 7028418221 Goa Call Girls Service North Goa Escorts
Cash Payment Contact:- 7028418221 Goa Call Girls Service North Goa Escorts
 
Kolkata Call Girl Bara Bazar 👉 8250192130 ❣️💯 Available With Room 24×7
Kolkata Call Girl Bara Bazar 👉 8250192130 ❣️💯 Available With Room 24×7Kolkata Call Girl Bara Bazar 👉 8250192130 ❣️💯 Available With Room 24×7
Kolkata Call Girl Bara Bazar 👉 8250192130 ❣️💯 Available With Room 24×7
 
Call Girls Chirag Delhi Delhi WhatsApp Number 9711199171
Call Girls Chirag Delhi Delhi WhatsApp Number 9711199171Call Girls Chirag Delhi Delhi WhatsApp Number 9711199171
Call Girls Chirag Delhi Delhi WhatsApp Number 9711199171
 
VIP Call Girls In Goa 7028418221 Call Girls In Baga Beach Escorts Service
VIP Call Girls In Goa 7028418221 Call Girls In Baga Beach Escorts ServiceVIP Call Girls In Goa 7028418221 Call Girls In Baga Beach Escorts Service
VIP Call Girls In Goa 7028418221 Call Girls In Baga Beach Escorts Service
 
Kolkata Call Girl Airport Kolkata 👉 8250192130 ❣️💯 Available With Room 24×7
Kolkata Call Girl Airport Kolkata 👉 8250192130 ❣️💯 Available With Room 24×7Kolkata Call Girl Airport Kolkata 👉 8250192130 ❣️💯 Available With Room 24×7
Kolkata Call Girl Airport Kolkata 👉 8250192130 ❣️💯 Available With Room 24×7
 
Call Girls Somajiguda Sarani 7001305949 all area service COD available Any Time
Call Girls Somajiguda Sarani 7001305949 all area service COD available Any TimeCall Girls Somajiguda Sarani 7001305949 all area service COD available Any Time
Call Girls Somajiguda Sarani 7001305949 all area service COD available Any Time
 
Beyond Bar & Club Udaipur CaLL GiRLS 09602870969
Beyond Bar & Club Udaipur CaLL GiRLS 09602870969Beyond Bar & Club Udaipur CaLL GiRLS 09602870969
Beyond Bar & Club Udaipur CaLL GiRLS 09602870969
 
Call Girls Nashik Gayatri 7001305949 Independent Escort Service Nashik
Call Girls Nashik Gayatri 7001305949 Independent Escort Service NashikCall Girls Nashik Gayatri 7001305949 Independent Escort Service Nashik
Call Girls Nashik Gayatri 7001305949 Independent Escort Service Nashik
 
Kolkata Call Girl Howrah 👉 8250192130 ❣️💯 Available With Room 24×7
Kolkata Call Girl Howrah 👉 8250192130 ❣️💯 Available With Room 24×7Kolkata Call Girl Howrah 👉 8250192130 ❣️💯 Available With Room 24×7
Kolkata Call Girl Howrah 👉 8250192130 ❣️💯 Available With Room 24×7
 
(Dipika) Call Girls in Bangur ! 8250192130 ₹2999 Only and Free Hotel Delivery...
(Dipika) Call Girls in Bangur ! 8250192130 ₹2999 Only and Free Hotel Delivery...(Dipika) Call Girls in Bangur ! 8250192130 ₹2999 Only and Free Hotel Delivery...
(Dipika) Call Girls in Bangur ! 8250192130 ₹2999 Only and Free Hotel Delivery...
 
(DIVYA) Dhanori Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(DIVYA) Dhanori Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(DIVYA) Dhanori Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(DIVYA) Dhanori Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
(KRITI) Pimpri Chinchwad Call Girls Just Call 7001035870 [ Cash on Delivery ]...
(KRITI) Pimpri Chinchwad Call Girls Just Call 7001035870 [ Cash on Delivery ]...(KRITI) Pimpri Chinchwad Call Girls Just Call 7001035870 [ Cash on Delivery ]...
(KRITI) Pimpri Chinchwad Call Girls Just Call 7001035870 [ Cash on Delivery ]...
 

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.