SlideShare a Scribd company logo
By
VINOTH R
Asst. professor
Dept. of Computer Application
St.Joseph’s college(Arts & Science)
Kovur, Chennai
TOPIC INCLUDES:
 Introduction to Thread
 Creation of Thread
 Life cycle of Thread
 Stopping and Blocking a Thread
 Using Thread Methods
 Thread Priority
 Thread Synchronization
 DeadLock
INTRODUCTION TO 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 Contd.
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.
MULTITHREADINGContd.
MULTITHREADING Contd.
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
CREATING THREAD
• 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 Contd.
CREATING THREAD
1. By extending Thread class
2. By implementing Runnable
interface
CREATINGTHREAD Contd.
1. By Extending Thread class
class Multi extends Thread // Extending thread class
{
public void run() // run() method declared
{
System.out.println("thread is running...");
}
public static void main(String args[])
{
Multi t1=new Multi(); //object initiated
t1.start(); // run() method called through start()
}
}
Output: thread is running…
CREATING THREAD Contd.
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 Contd.
2. By implementing Runnable interface
class Multi3 implements Runnable // Implementing Runnable interface
{
public void run()
{
System.out.println("thread is running...");
}
public static void main(String args[])
{
Multi3 m1=new Multi3(); // object initiated for class
Thread t1 =new Thread(m1); // object initiated for thread
t1.start();
} }
Output: thread is running…
LIFE cycle of 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
LIFE cycle of a thread contd.
LIFE cycle of a thread contd.
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.
LIFE cycle of a thread contd.
Runnable State:
 The thread is ready for execution
 Waiting for the availability of the processor.
 The thread has joined the queue
LIFE cycle of a thread contd.
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.
LIFE cycle of a thread contd.
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.
LIFE cycle of a thread contd.
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.
Thread methods
• 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
Stopping and blocking
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();
Stopping and blocking
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
Thread priority
• 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);
Thread priority contd.
• 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.
Java synchronization
• 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.
Java synchronization contd.
• 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

What's hot

Threads in JAVA
Threads in JAVAThreads in JAVA
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
Tanmoy Barman
 
Applets
AppletsApplets
Java Streams
Java StreamsJava Streams
Java Streams
M Vishnuvardhan Reddy
 
Life cycle-of-a-thread
Life cycle-of-a-threadLife cycle-of-a-thread
Life cycle-of-a-thread
javaicon
 
Java string handling
Java string handlingJava string handling
Java string handling
Salman Khan
 
Java swing
Java swingJava swing
Java swing
Apurbo Datta
 
Java package
Java packageJava package
Java package
CS_GDRCST
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
VINOTH R
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
Hamid Ghorbani
 
Java multi threading
Java multi threadingJava multi threading
Java multi threading
Raja Sekhar
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
RahulAnanda1
 
Java awt (abstract window toolkit)
Java awt (abstract window toolkit)Java awt (abstract window toolkit)
Java awt (abstract window toolkit)
Elizabeth alexander
 
Servlet and servlet life cycle
Servlet and servlet life cycleServlet and servlet life cycle
Servlet and servlet life cycle
Dhruvin Nakrani
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in java
CPD INDIA
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
CPD INDIA
 
Introduction to method overloading & method overriding in java hdm
Introduction to method overloading & method overriding  in java  hdmIntroduction to method overloading & method overriding  in java  hdm
Introduction to method overloading & method overriding in java hdm
Harshal Misalkar
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Javaparag
 
Multithreading in Java
Multithreading in JavaMultithreading in Java
Multithreading in Java
Jayant Dalvi
 

What's hot (20)

Threads in JAVA
Threads in JAVAThreads in JAVA
Threads in JAVA
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
 
Applets
AppletsApplets
Applets
 
Java Streams
Java StreamsJava Streams
Java Streams
 
Life cycle-of-a-thread
Life cycle-of-a-threadLife cycle-of-a-thread
Life cycle-of-a-thread
 
Java string handling
Java string handlingJava string handling
Java string handling
 
Java swing
Java swingJava swing
Java swing
 
Java package
Java packageJava package
Java package
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
Java multi threading
Java multi threadingJava multi threading
Java multi threading
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Java awt (abstract window toolkit)
Java awt (abstract window toolkit)Java awt (abstract window toolkit)
Java awt (abstract window toolkit)
 
Servlet and servlet life cycle
Servlet and servlet life cycleServlet and servlet life cycle
Servlet and servlet life cycle
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in java
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
 
Introduction to method overloading & method overriding in java hdm
Introduction to method overloading & method overriding  in java  hdmIntroduction to method overloading & method overriding  in java  hdm
Introduction to method overloading & method overriding in java hdm
 
OOP java
OOP javaOOP java
OOP java
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
 
Multithreading in Java
Multithreading in JavaMultithreading in Java
Multithreading in Java
 

Similar to MULTI THREADING IN JAVA

Multithreadingppt.pptx
Multithreadingppt.pptxMultithreadingppt.pptx
Multithreadingppt.pptx
HKShab
 
econtent thread in java.pptx
econtent thread in java.pptxecontent thread in java.pptx
econtent thread in java.pptx
ramyan49
 
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
SaikiranBiradar3
 
Java threading
Java threadingJava threading
Java threading
Chinh Ngo Nguyen
 
Chap2 2 1
Chap2 2 1Chap2 2 1
Chap2 2 1
Hemo Chella
 
Multithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of threadMultithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of thread
Kartik Dube
 
U4 JAVA.pptx
U4 JAVA.pptxU4 JAVA.pptx
U4 JAVA.pptx
madan r
 
Threads in Java
Threads in JavaThreads in Java
Threads in Java
HarshaDokula
 
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
nimbalkarvikram966
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Arafat Hossan
 
multithreading.pptx
multithreading.pptxmultithreading.pptx
multithreading.pptx
Sravanibitragunta
 
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
Arulmozhivarman8
 
Multi threading
Multi threadingMulti threading
Multi threading
gndu
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Monika Mishra
 
Threads in java, Multitasking and Multithreading
Threads in java, Multitasking and MultithreadingThreads in java, Multitasking and Multithreading
Threads in java, Multitasking and Multithreading
ssusere538f7
 
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
SivaSankari36
 
Threading concepts
Threading conceptsThreading concepts
Threading concepts
Raheemaparveen
 
Basic of Multithreading in JAva
Basic of Multithreading in JAvaBasic of Multithreading in JAva
Basic of Multithreading in JAva
suraj pandey
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Lovely Professional University
 

Similar to MULTI THREADING IN JAVA (20)

Multithreadingppt.pptx
Multithreadingppt.pptxMultithreadingppt.pptx
Multithreadingppt.pptx
 
econtent thread in java.pptx
econtent thread in java.pptxecontent thread in java.pptx
econtent thread in java.pptx
 
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
 
Multithreading.pptx
Multithreading.pptxMultithreading.pptx
Multithreading.pptx
 
Java threading
Java threadingJava threading
Java threading
 
Chap2 2 1
Chap2 2 1Chap2 2 1
Chap2 2 1
 
Multithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of threadMultithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of thread
 
U4 JAVA.pptx
U4 JAVA.pptxU4 JAVA.pptx
U4 JAVA.pptx
 
Threads in Java
Threads in JavaThreads in Java
Threads in Java
 
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
multithreading.pptx
multithreading.pptxmultithreading.pptx
multithreading.pptx
 
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
 
Multi threading
Multi threadingMulti threading
Multi threading
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Threads in java, Multitasking and Multithreading
Threads in java, Multitasking and MultithreadingThreads in java, Multitasking and Multithreading
Threads in java, Multitasking and Multithreading
 
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
 
Threading concepts
Threading conceptsThreading concepts
Threading concepts
 
Basic of Multithreading in JAva
Basic of Multithreading in JAvaBasic of Multithreading in JAva
Basic of Multithreading in JAva
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 

Recently uploaded

1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 

Recently uploaded (20)

1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 

MULTI THREADING IN JAVA

  • 1. By VINOTH R Asst. professor Dept. of Computer Application St.Joseph’s college(Arts & Science) Kovur, Chennai
  • 2. TOPIC INCLUDES:  Introduction to Thread  Creation of Thread  Life cycle of Thread  Stopping and Blocking a Thread  Using Thread Methods  Thread Priority  Thread Synchronization  DeadLock
  • 3. INTRODUCTION TO 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 Contd. 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. CREATING THREAD • 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 Contd. CREATING THREAD 1. By extending Thread class 2. By implementing Runnable interface
  • 10. CREATINGTHREAD Contd. 1. By Extending Thread class class Multi extends Thread // Extending thread class { public void run() // run() method declared { System.out.println("thread is running..."); } public static void main(String args[]) { Multi t1=new Multi(); //object initiated t1.start(); // run() method called through start() } } Output: thread is running…
  • 11. CREATING THREAD Contd. 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 Contd. 2. By implementing Runnable interface class Multi3 implements Runnable // Implementing Runnable interface { public void run() { System.out.println("thread is running..."); } public static void main(String args[]) { Multi3 m1=new Multi3(); // object initiated for class Thread t1 =new Thread(m1); // object initiated for thread t1.start(); } } Output: thread is running…
  • 13. LIFE cycle of 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
  • 14. LIFE cycle of a thread contd.
  • 15. LIFE cycle of a thread contd. 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. LIFE cycle of a thread contd. Runnable State:  The thread is ready for execution  Waiting for the availability of the processor.  The thread has joined the queue
  • 17. LIFE cycle of a thread contd. 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. LIFE cycle of a thread contd. 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. LIFE cycle of a thread contd. 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. Thread methods • 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. Stopping and blocking 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. Stopping and blocking 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. Thread priority • 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. Thread priority contd. • 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. Java synchronization • 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. Java synchronization contd. • 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.