SlideShare a Scribd company logo
- Arulkumar V
Assistant Professor, SECE
Thread
Java – Thread:
12/19/20172
 A thread is a lightweight sub process, a smallest
unit of processing.
 It is a separate path of execution.
 Threads are independent, if there occurs exception
in one thread, it doesn't affect other threads.
 It shares a common memory area of a process.
Note: At a time one thread is executed only.
Java Thread – cont..,
12/19/20173
Life cycle of a Thread:
12/19/20174
 A thread can be in one of the five states.
 The life cycle of the thread in java is controlled by
JVM. The java thread states are as follows:
New
Runnable
Running
Non-Runnable (Blocked)
Terminated
Life cycle of a Thread:
12/19/20175
States of a Thread:
12/19/20176
1) New: The thread is in new state if you create an
instance of Thread class but before the invocation of start()
method.
2) Runnable: The thread is in runnable state after
invocation of start() method, but the thread scheduler has
not selected it to be the running thread.
3) Running: The thread is in running state if the thread
scheduler has selected it.
4) Non-Runnable (Blocked): This is the state when the
thread is still alive, but is currently not eligible to run.
5) Terminated: A thread is in terminated or dead state
when its run() method exits.
Creating thread:
12/19/20177
There are two ways to create a thread:
 By extending Thread class
 By implementing Runnable interface.
Thread class:
12/19/20178
 Thread class provide constructors and methods to
create and perform operations on a thread.
 Thread class extends Object class and implements
Runnable interface.
 Commonly used Constructors of Thread class:
 Thread()
 Thread(String name)
 Thread(Runnable r)
 Thread(Runnable r, String name)
Commonly used methods of
Thread class:
12/19/20179
 public void run(): is used to perform action for a
thread.
 public void start(): starts the execution of the
thread. JVM calls the run() method on the thread.
 public void sleep(long miliseconds): Causes
the currently executing thread to sleep (temporarily
cease execution) for the specified number of
milliseconds.
 public void join(): waits for a thread to die.
Starting a thread:
12/19/201710
start() method of Thread class is used to start a
newly created thread. It performs following tasks:
 A new thread starts.
 The thread moves from New state to the Runnable
state.
 When the thread gets a chance to execute, its
target run() method will run.
1)By extending Thread class:
12/19/201711
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….
2)By implementing the Runnable
interface:
12/19/201712
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();
}
}
Output:
Thread is running….
Multithreading in Java
12/19/201713
 Multithreading in java is a process of executing
multiple threads simultaneously.
 Thread is basically a lightweight sub-process, a
smallest unit of processing.
 Multiprocessing and multithreading, both are used to
achieve multitasking.

Advantages of Java Multithreading
12/19/201714
 1) It doesn't block the user because threads are
independent and you can perform multiple operations
at same time.
 2) You can perform many operations together so it
saves time.
 3) Threads are independent so it doesn't affect other
threads if exception occur in a single thread.
12/19/201715
class RunnableDemo implements Runnable {
private Thread t;
private String threadName;
RunnableDemo( String name) { t
hreadName = name;
System.out.println("Creating " + threadName );
}
public void run()
{ System.out.println("Running " + threadName );
try {
for(int i = 4; i > 0; i--)
{
System.out.println("Thread: " + threadName + ", " + i); //
Let the thread sleep for a while.
Thread.sleep(50); }
12/19/201716
catch (InterruptedException e)
{
System.out.println("Thread " + threadName + "
interrupted.");
}
System.out.println("Thread " + threadName + " exiting.");
}
public void start ()
{ System.out.println("Starting " + threadName );
if (t == null)
{
t = new Thread (this, threadName);
t.start (); } } }
12/19/201717
public class TestThread
{
public static void main(String args[])
{
RunnableDemo R1 = new RunnableDemo( "Thread-1");
R1.start(); RunnableDemo R2 = new RunnableDemo(
"Thread-2");
R2.start();
}
}
12/19/201718
Output
Creating Thread-1
Starting Thread-1
Creating Thread-2
Starting Thread-2
Running Thread-1
Thread: Thread-1, 4
Running Thread-2
Thread: Thread-2, 4
Thread: Thread-1, 3
Thread: Thread-2, 3
Thread: Thread-1, 2
Thread: Thread-2, 2
Thread: Thread-1, 1
Thread: Thread-2, 1
Thread Thread-1 exiting.
Thread Thread-2 exiting.
12/19/201719
12/19/201720
Thank You

More Related Content

What's hot

javathreads
javathreadsjavathreads
javathreads
Arjun Shanka
 
Thread
ThreadThread
Thread presentation
Thread presentationThread presentation
Thread presentation
AAshish Ojha
 
Thread model of java
Thread model of javaThread model of java
Thread model of java
myrajendra
 
Java threading
Java threadingJava threading
Java threading
Chinh Ngo Nguyen
 
Threads in JAVA
Threads in JAVAThreads in JAVA
Multithread
MultithreadMultithread
Multithread
vuhaininh88
 
Threads in Java
Threads in JavaThreads in Java
Threads in Java
Gaurav Aggarwal
 
Java Threads and Concurrency
Java Threads and ConcurrencyJava Threads and Concurrency
Java Threads and Concurrency
Sunil OS
 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread Synchronization
Benj Del Mundo
 
Learning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and SynchronizationLearning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and Synchronization
caswenson
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Arafat Hossan
 
Java threads
Java threadsJava threads
Java Thread & Multithreading
Java Thread & MultithreadingJava Thread & Multithreading
Java Thread & Multithreading
jehan1987
 
Multithread Programing in Java
Multithread Programing in JavaMultithread Programing in Java
Multithread Programing in Java
M. Raihan
 
Thread
ThreadThread
Thread
Juhi Kumari
 
multi threading
multi threadingmulti threading
Java unit 12
Java unit 12Java unit 12
Java unit 12
Shipra Swati
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
parag
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
junnubabu
 

What's hot (20)

javathreads
javathreadsjavathreads
javathreads
 
Thread
ThreadThread
Thread
 
Thread presentation
Thread presentationThread presentation
Thread presentation
 
Thread model of java
Thread model of javaThread model of java
Thread model of java
 
Java threading
Java threadingJava threading
Java threading
 
Threads in JAVA
Threads in JAVAThreads in JAVA
Threads in JAVA
 
Multithread
MultithreadMultithread
Multithread
 
Threads in Java
Threads in JavaThreads in Java
Threads in Java
 
Java Threads and Concurrency
Java Threads and ConcurrencyJava Threads and Concurrency
Java Threads and Concurrency
 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread Synchronization
 
Learning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and SynchronizationLearning Java 3 – Threads and Synchronization
Learning Java 3 – Threads and Synchronization
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Java threads
Java threadsJava threads
Java threads
 
Java Thread & Multithreading
Java Thread & MultithreadingJava Thread & Multithreading
Java Thread & Multithreading
 
Multithread Programing in Java
Multithread Programing in JavaMultithread Programing in Java
Multithread Programing in Java
 
Thread
ThreadThread
Thread
 
multi threading
multi threadingmulti threading
multi threading
 
Java unit 12
Java unit 12Java unit 12
Java unit 12
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 

Similar to Thread&multithread

Class notes(week 9) on multithreading
Class notes(week 9) on multithreadingClass notes(week 9) on multithreading
Class notes(week 9) on multithreading
Kuntal Bhowmick
 
Java Threads
Java ThreadsJava Threads
Java Threads
Hamid Ghorbani
 
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
arnavytstudio2814
 
Java Threads And Concurrency Presentation. 2024
Java Threads And Concurrency Presentation. 2024Java Threads And Concurrency Presentation. 2024
Java Threads And Concurrency Presentation. 2024
kashyapneha2809
 
Java-Threads And Concurrency Presentation. 2024
Java-Threads And Concurrency Presentation. 2024Java-Threads And Concurrency Presentation. 2024
Java-Threads And Concurrency Presentation. 2024
nehakumari0xf
 
BCA MultiThreading.ppt
BCA MultiThreading.pptBCA MultiThreading.ppt
BCA MultiThreading.ppt
sarthakgithub
 
Slide 7 Thread-1.pptx
Slide 7 Thread-1.pptxSlide 7 Thread-1.pptx
Slide 7 Thread-1.pptx
ajmalhamidi1380
 
Threadnotes
ThreadnotesThreadnotes
Threadnotes
Himanshu Rajput
 
multithreading, creating a thread and life cycle in java.ppt
multithreading, creating a thread and life cycle in java.pptmultithreading, creating a thread and life cycle in java.ppt
multithreading, creating a thread and life cycle in java.ppt
shikhaverma566116
 
Thread Concept: Multithreading, Creating thread using thread
Thread Concept: Multithreading, Creating thread using threadThread Concept: Multithreading, Creating thread using thread
Thread Concept: Multithreading, Creating thread using thread
poongothai11
 
Multi threading
Multi threadingMulti threading
Multi threading
Mavoori Soshmitha
 
Multi-threaded Programming in JAVA
Multi-threaded Programming in JAVAMulti-threaded Programming in JAVA
Multi-threaded Programming in JAVA
Vikram Kalyani
 
Chap2 2 1
Chap2 2 1Chap2 2 1
Chap2 2 1
Hemo Chella
 
Internet Programming with Java
Internet Programming with JavaInternet Programming with Java
Internet Programming with Java
kavitha muneeshwaran
 
Java
JavaJava
Unit-3 MULTITHREADING-2.pdf
Unit-3 MULTITHREADING-2.pdfUnit-3 MULTITHREADING-2.pdf
Unit-3 MULTITHREADING-2.pdf
GouthamSoma1
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Arafat Hossan
 
Multithreading programming in java
Multithreading programming in javaMultithreading programming in java
Multithreading programming in java
Elizabeth alexander
 
Multithreading in Java is a process of executing.pptx
Multithreading in Java is a process of executing.pptxMultithreading in Java is a process of executing.pptx
Multithreading in Java is a process of executing.pptx
PavanKumarPNVS
 
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
 

Similar to Thread&multithread (20)

Class notes(week 9) on multithreading
Class notes(week 9) on multithreadingClass notes(week 9) on multithreading
Class notes(week 9) on multithreading
 
Java Threads
Java ThreadsJava Threads
Java Threads
 
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
 
Java Threads And Concurrency Presentation. 2024
Java Threads And Concurrency Presentation. 2024Java Threads And Concurrency Presentation. 2024
Java Threads And Concurrency Presentation. 2024
 
Java-Threads And Concurrency Presentation. 2024
Java-Threads And Concurrency Presentation. 2024Java-Threads And Concurrency Presentation. 2024
Java-Threads And Concurrency Presentation. 2024
 
BCA MultiThreading.ppt
BCA MultiThreading.pptBCA MultiThreading.ppt
BCA MultiThreading.ppt
 
Slide 7 Thread-1.pptx
Slide 7 Thread-1.pptxSlide 7 Thread-1.pptx
Slide 7 Thread-1.pptx
 
Threadnotes
ThreadnotesThreadnotes
Threadnotes
 
multithreading, creating a thread and life cycle in java.ppt
multithreading, creating a thread and life cycle in java.pptmultithreading, creating a thread and life cycle in java.ppt
multithreading, creating a thread and life cycle in java.ppt
 
Thread Concept: Multithreading, Creating thread using thread
Thread Concept: Multithreading, Creating thread using threadThread Concept: Multithreading, Creating thread using thread
Thread Concept: Multithreading, Creating thread using thread
 
Multi threading
Multi threadingMulti threading
Multi threading
 
Multi-threaded Programming in JAVA
Multi-threaded Programming in JAVAMulti-threaded Programming in JAVA
Multi-threaded Programming in JAVA
 
Chap2 2 1
Chap2 2 1Chap2 2 1
Chap2 2 1
 
Internet Programming with Java
Internet Programming with JavaInternet Programming with Java
Internet Programming with Java
 
Java
JavaJava
Java
 
Unit-3 MULTITHREADING-2.pdf
Unit-3 MULTITHREADING-2.pdfUnit-3 MULTITHREADING-2.pdf
Unit-3 MULTITHREADING-2.pdf
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Multithreading programming in java
Multithreading programming in javaMultithreading programming in java
Multithreading programming in java
 
Multithreading in Java is a process of executing.pptx
Multithreading in Java is a process of executing.pptxMultithreading in Java is a process of executing.pptx
Multithreading in Java is a process of executing.pptx
 
Multithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of threadMultithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of thread
 

More from PhD Research Scholar

Ajax
AjaxAjax
Quiz servlet
Quiz servletQuiz servlet
Quiz servlet
PhD Research Scholar
 
Quiz
QuizQuiz
servlet db connectivity
servlet db connectivityservlet db connectivity
servlet db connectivity
PhD Research Scholar
 
2.java script dom
2.java script  dom2.java script  dom
2.java script dom
PhD Research Scholar
 
1.java script
1.java script1.java script
1.java script
PhD Research Scholar
 
Quiz javascript
Quiz javascriptQuiz javascript
Quiz javascript
PhD Research Scholar
 
String
StringString
Streams&io
Streams&ioStreams&io
Streams&io
PhD Research Scholar
 
Packages
PackagesPackages
Interface in java
Interface in javaInterface in java
Interface in java
PhD Research Scholar
 
Inner classes in java
Inner classes in javaInner classes in java
Inner classes in java
PhD Research Scholar
 
Inheritance
InheritanceInheritance
Exception handling
Exception handlingException handling
Exception handling
PhD Research Scholar
 
Applets
AppletsApplets
Abstract class
Abstract classAbstract class
Abstract class
PhD Research Scholar
 
7. tuples, set & dictionary
7. tuples, set & dictionary7. tuples, set & dictionary
7. tuples, set & dictionary
PhD Research Scholar
 
6. list
6. list6. list
5. string
5. string5. string
4. functions
4. functions4. functions
4. functions
PhD Research Scholar
 

More from PhD Research Scholar (20)

Ajax
AjaxAjax
Ajax
 
Quiz servlet
Quiz servletQuiz servlet
Quiz servlet
 
Quiz
QuizQuiz
Quiz
 
servlet db connectivity
servlet db connectivityservlet db connectivity
servlet db connectivity
 
2.java script dom
2.java script  dom2.java script  dom
2.java script dom
 
1.java script
1.java script1.java script
1.java script
 
Quiz javascript
Quiz javascriptQuiz javascript
Quiz javascript
 
String
StringString
String
 
Streams&io
Streams&ioStreams&io
Streams&io
 
Packages
PackagesPackages
Packages
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Inner classes in java
Inner classes in javaInner classes in java
Inner classes in java
 
Inheritance
InheritanceInheritance
Inheritance
 
Exception handling
Exception handlingException handling
Exception handling
 
Applets
AppletsApplets
Applets
 
Abstract class
Abstract classAbstract class
Abstract class
 
7. tuples, set & dictionary
7. tuples, set & dictionary7. tuples, set & dictionary
7. tuples, set & dictionary
 
6. list
6. list6. list
6. list
 
5. string
5. string5. string
5. string
 
4. functions
4. functions4. functions
4. functions
 

Recently uploaded

How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
Celine George
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
haiqairshad
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
สมใจ จันสุกสี
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
siemaillard
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
TechSoup
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Denish Jangid
 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
PsychoTech Services
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
Himanshu Rai
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
adhitya5119
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
Katrina Pritchard
 
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching AptitudeUGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
S. Raj Kumar
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 
IGCSE Biology Chapter 14- Reproduction in Plants.pdf
IGCSE Biology Chapter 14- Reproduction in Plants.pdfIGCSE Biology Chapter 14- Reproduction in Plants.pdf
IGCSE Biology Chapter 14- Reproduction in Plants.pdf
Amin Marwan
 
math operations ued in python and all used
math operations ued in python and all usedmath operations ued in python and all used
math operations ued in python and all used
ssuser13ffe4
 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
Krassimira Luka
 

Recently uploaded (20)

How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17How to Make a Field Mandatory in Odoo 17
How to Make a Field Mandatory in Odoo 17
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
คำศัพท์ คำพื้นฐานการอ่าน ภาษาอังกฤษ ระดับชั้น ม.1
 
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptxPrésentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
Présentationvvvvvvvvvvvvvvvvvvvvvvvvvvvv2.pptx
 
Walmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdfWalmart Business+ and Spark Good for Nonprofits.pdf
Walmart Business+ and Spark Good for Nonprofits.pdf
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
 
Main Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docxMain Java[All of the Base Concepts}.docx
Main Java[All of the Base Concepts}.docx
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
 
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching AptitudeUGC NET Exam Paper 1- Unit 1:Teaching Aptitude
UGC NET Exam Paper 1- Unit 1:Teaching Aptitude
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 
IGCSE Biology Chapter 14- Reproduction in Plants.pdf
IGCSE Biology Chapter 14- Reproduction in Plants.pdfIGCSE Biology Chapter 14- Reproduction in Plants.pdf
IGCSE Biology Chapter 14- Reproduction in Plants.pdf
 
math operations ued in python and all used
math operations ued in python and all usedmath operations ued in python and all used
math operations ued in python and all used
 
Temple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation resultsTemple of Asclepius in Thrace. Excavation results
Temple of Asclepius in Thrace. Excavation results
 

Thread&multithread

  • 1. - Arulkumar V Assistant Professor, SECE Thread
  • 2. Java – Thread: 12/19/20172  A thread is a lightweight sub process, a smallest unit of processing.  It is a separate path of execution.  Threads are independent, if there occurs exception in one thread, it doesn't affect other threads.  It shares a common memory area of a process. Note: At a time one thread is executed only.
  • 3. Java Thread – cont.., 12/19/20173
  • 4. Life cycle of a Thread: 12/19/20174  A thread can be in one of the five states.  The life cycle of the thread in java is controlled by JVM. The java thread states are as follows: New Runnable Running Non-Runnable (Blocked) Terminated
  • 5. Life cycle of a Thread: 12/19/20175
  • 6. States of a Thread: 12/19/20176 1) New: The thread is in new state if you create an instance of Thread class but before the invocation of start() method. 2) Runnable: The thread is in runnable state after invocation of start() method, but the thread scheduler has not selected it to be the running thread. 3) Running: The thread is in running state if the thread scheduler has selected it. 4) Non-Runnable (Blocked): This is the state when the thread is still alive, but is currently not eligible to run. 5) Terminated: A thread is in terminated or dead state when its run() method exits.
  • 7. Creating thread: 12/19/20177 There are two ways to create a thread:  By extending Thread class  By implementing Runnable interface.
  • 8. Thread class: 12/19/20178  Thread class provide constructors and methods to create and perform operations on a thread.  Thread class extends Object class and implements Runnable interface.  Commonly used Constructors of Thread class:  Thread()  Thread(String name)  Thread(Runnable r)  Thread(Runnable r, String name)
  • 9. Commonly used methods of Thread class: 12/19/20179  public void run(): is used to perform action for a thread.  public void start(): starts the execution of the thread. JVM calls the run() method on the thread.  public void sleep(long miliseconds): Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds.  public void join(): waits for a thread to die.
  • 10. Starting a thread: 12/19/201710 start() method of Thread class is used to start a newly created thread. It performs following tasks:  A new thread starts.  The thread moves from New state to the Runnable state.  When the thread gets a chance to execute, its target run() method will run.
  • 11. 1)By extending Thread class: 12/19/201711 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….
  • 12. 2)By implementing the Runnable interface: 12/19/201712 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(); } } Output: Thread is running….
  • 13. Multithreading in Java 12/19/201713  Multithreading in java is a process of executing multiple threads simultaneously.  Thread is basically a lightweight sub-process, a smallest unit of processing.  Multiprocessing and multithreading, both are used to achieve multitasking. 
  • 14. Advantages of Java Multithreading 12/19/201714  1) It doesn't block the user because threads are independent and you can perform multiple operations at same time.  2) You can perform many operations together so it saves time.  3) Threads are independent so it doesn't affect other threads if exception occur in a single thread.
  • 15. 12/19/201715 class RunnableDemo implements Runnable { private Thread t; private String threadName; RunnableDemo( String name) { t hreadName = name; System.out.println("Creating " + threadName ); } public void run() { System.out.println("Running " + threadName ); try { for(int i = 4; i > 0; i--) { System.out.println("Thread: " + threadName + ", " + i); // Let the thread sleep for a while. Thread.sleep(50); }
  • 16. 12/19/201716 catch (InterruptedException e) { System.out.println("Thread " + threadName + " interrupted."); } System.out.println("Thread " + threadName + " exiting."); } public void start () { System.out.println("Starting " + threadName ); if (t == null) { t = new Thread (this, threadName); t.start (); } } }
  • 17. 12/19/201717 public class TestThread { public static void main(String args[]) { RunnableDemo R1 = new RunnableDemo( "Thread-1"); R1.start(); RunnableDemo R2 = new RunnableDemo( "Thread-2"); R2.start(); } }
  • 18. 12/19/201718 Output Creating Thread-1 Starting Thread-1 Creating Thread-2 Starting Thread-2 Running Thread-1 Thread: Thread-1, 4 Running Thread-2 Thread: Thread-2, 4 Thread: Thread-1, 3 Thread: Thread-2, 3 Thread: Thread-1, 2 Thread: Thread-2, 2 Thread: Thread-1, 1 Thread: Thread-2, 1 Thread Thread-1 exiting. Thread Thread-2 exiting.