SlideShare a Scribd company logo
Multithreading in Java
Thread
• A thread is a lightweight sub-process.
Cont…
• The smallest unit of processing.
• Threads are independent
Cont…
• If there occurs exception in one thread, it
doesn't affect other threads.
• It uses a shared memory area.
Multithreading
• Multithreading in Java is a process of
executing multiple threads simultaneously.
Advantages of Java Multithreading
• You can perform many operations
together, so it saves time.
Cont…
• It doesn't block the user because threads
are independent and you can perform
multiple operations at the same time.
• Threads are independent, so it doesn't
affect other threads if an exception occurs
in a single thread.
Application of Multi Threading
• Games
• Animations
Life cycle of a Thread
(Thread States)
• There are 5 states in the life cycle of
thread .
1. New
2. Runnable
3. Running
4. Non-Runnable (Blocked)
5. Terminated
Note: The life cycle of the thread in java
is controlled by JVM.
Diagram for Life Cycle of Thread
run()
Cont…
1. New:
The thread is in new state if you create
an instance (object) of Thread class but
before the calling of start() method.
Cont…
2. Runnable:
The thread is in runnable state after
calling of start() method, but the thread
scheduler has not selected it to be the
running thread.
Cont…
3. Running:
The thread is in running state if the
thread scheduler has selected it and in
this stage run() method is called.
Cont…
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.
How to create thread
• There are two ways to create a thread:
1. By extending Thread class
2. By implementing Runnable interface.
1. By extending Thread class
Thread class:
• Thread class provide constructors and
methods to create and perform operations
on a thread.
• Thread class extends Object class and
implements Runnable interface.
Constructors of Thread class
• Thread()
• Thread(String name)
Methods of Thread class
1. public void start(): starts the execution of the
thread. JVM calls the run() method of the thread.
2. public void run(): is used to perform action for a
thread. It is declared in Runnable Interface
Cont…
3. public static void sleep(long milis): It sleeps a
thread for the specified amount of time.
4. public void final setPriority(int newPriority): It
set the priority of the thread.
5. public int getPriority(): It returns the priority of
the thread.
Example
class A extends Thread
{
public void run()
{
System.out.println(“Thread is running”);
}
public static void main(String a[])
{
A t1 = new A();
t1.start();
}
}
Output: Thread is running
2. By implementing Runnable interface.
Runnable interface:
• It contains only run() Method.
Example
class A implements Runnable
{
public void run()
{
System.out.println(“Runnable Interface”);
}
public static void main(String a[])
{
A t1 = new A();
Thread t2 = new Thread(t1);
t2.start();
}
}
Output:
Runnable Interface
Priority of a Thread
• Each thread has a priority.
• Priorities are represented by a number between 1 and
10.
• Default priority of a thread is 5
Cont…
• public final void setPriority(int newPriority): It set
the priority of the thread which is 1 (minimum) to 10
(maximum).
• 3 constants defined in Thread class:
– public static int MIN_PRIORITY = 1
– public static int NORM_PRIORITY = 5
– public static int MAX_PRIORITY = 10
Cont…
• public int getPriority(): It returns the priority
of the thread.
Example
class A extends Thread
{
public static void main(String a[])
{
A t1= new A();
t1.setPriority(Thread. MAX_PRIORITY);
System.out.println(t1.getPriority());
}
}
Output: 10

More Related Content

Similar to BCA MultiThreading.ppt

Unit-3 MULTITHREADING-2.pdf
Unit-3 MULTITHREADING-2.pdfUnit-3 MULTITHREADING-2.pdf
Unit-3 MULTITHREADING-2.pdf
GouthamSoma1
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
VINOTH R
 
1.17 Thread in java.pptx
1.17 Thread in java.pptx1.17 Thread in java.pptx
1.17 Thread in java.pptx
TREXSHyNE
 
Multi threading
Multi threadingMulti threading
Multi threading
Mavoori Soshmitha
 
Chap2 2 1
Chap2 2 1Chap2 2 1
Chap2 2 1
Hemo Chella
 
Thread&multithread
Thread&multithreadThread&multithread
Thread&multithread
PhD Research Scholar
 
Slide 7 Thread-1.pptx
Slide 7 Thread-1.pptxSlide 7 Thread-1.pptx
Slide 7 Thread-1.pptx
ajmalhamidi1380
 
econtent thread in java.pptx
econtent thread in java.pptxecontent thread in java.pptx
econtent thread in java.pptx
ramyan49
 
Threads in java, Multitasking and Multithreading
Threads in java, Multitasking and MultithreadingThreads in java, Multitasking and Multithreading
Threads in java, Multitasking and Multithreading
ssusere538f7
 
Java threads
Java threadsJava threads
Java threads
Prabhakaran V M
 
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
 
Java class 6
Java class 6Java class 6
Java class 6
Edureka!
 
Java threading
Java threadingJava threading
Java threading
Chinh Ngo Nguyen
 
Java multi threading
Java multi threadingJava multi threading
Java multi threading
Raja Sekhar
 
Multithreading programming in java
Multithreading programming in javaMultithreading programming in java
Multithreading programming in java
Elizabeth alexander
 
Multithreadingppt.pptx
Multithreadingppt.pptxMultithreadingppt.pptx
Multithreadingppt.pptx
HKShab
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Arafat Hossan
 
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
 
Java Threads
Java ThreadsJava Threads
Java Threads
Hamid Ghorbani
 

Similar to BCA MultiThreading.ppt (20)

Unit-3 MULTITHREADING-2.pdf
Unit-3 MULTITHREADING-2.pdfUnit-3 MULTITHREADING-2.pdf
Unit-3 MULTITHREADING-2.pdf
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
 
1.17 Thread in java.pptx
1.17 Thread in java.pptx1.17 Thread in java.pptx
1.17 Thread in java.pptx
 
Multi threading
Multi threadingMulti threading
Multi threading
 
Chap2 2 1
Chap2 2 1Chap2 2 1
Chap2 2 1
 
Thread&multithread
Thread&multithreadThread&multithread
Thread&multithread
 
Slide 7 Thread-1.pptx
Slide 7 Thread-1.pptxSlide 7 Thread-1.pptx
Slide 7 Thread-1.pptx
 
econtent thread in java.pptx
econtent thread in java.pptxecontent thread in java.pptx
econtent thread in java.pptx
 
Threads in java, Multitasking and Multithreading
Threads in java, Multitasking and MultithreadingThreads in java, Multitasking and Multithreading
Threads in java, Multitasking and Multithreading
 
Java threads
Java threadsJava threads
Java threads
 
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
 
Java class 6
Java class 6Java class 6
Java class 6
 
Java threading
Java threadingJava threading
Java threading
 
Java multi threading
Java multi threadingJava multi threading
Java multi threading
 
Multithreading programming in java
Multithreading programming in javaMultithreading programming in java
Multithreading programming in java
 
Multithreadingppt.pptx
Multithreadingppt.pptxMultithreadingppt.pptx
Multithreadingppt.pptx
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
OOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptxOOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptx
 
Java Threads
Java ThreadsJava Threads
Java Threads
 

More from sarthakgithub

Access Modifiers in Java.pptx
 Access Modifiers in Java.pptx Access Modifiers in Java.pptx
Access Modifiers in Java.pptx
sarthakgithub
 
ppt-2.pptx
ppt-2.pptxppt-2.pptx
ppt-2.pptx
sarthakgithub
 
soumay.pptx
soumay.pptxsoumay.pptx
soumay.pptx
sarthakgithub
 
Data Path Design and Bus Organization.pptx
Data Path Design and Bus Organization.pptxData Path Design and Bus Organization.pptx
Data Path Design and Bus Organization.pptx
sarthakgithub
 
Python Unit II.pptx
Python Unit II.pptxPython Unit II.pptx
Python Unit II.pptx
sarthakgithub
 
OOP-Module-1-Section-4-LectureNo1-5.pptx
OOP-Module-1-Section-4-LectureNo1-5.pptxOOP-Module-1-Section-4-LectureNo1-5.pptx
OOP-Module-1-Section-4-LectureNo1-5.pptx
sarthakgithub
 
Flow Control (1).ppt
Flow Control (1).pptFlow Control (1).ppt
Flow Control (1).ppt
sarthakgithub
 
BCA Abstraction.pptx
BCA Abstraction.pptxBCA Abstraction.pptx
BCA Abstraction.pptx
sarthakgithub
 
BCA Final Keyword.pptx
BCA Final Keyword.pptxBCA Final Keyword.pptx
BCA Final Keyword.pptx
sarthakgithub
 
BCA Super Keyword.pptx
BCA Super Keyword.pptxBCA Super Keyword.pptx
BCA Super Keyword.pptx
sarthakgithub
 

More from sarthakgithub (10)

Access Modifiers in Java.pptx
 Access Modifiers in Java.pptx Access Modifiers in Java.pptx
Access Modifiers in Java.pptx
 
ppt-2.pptx
ppt-2.pptxppt-2.pptx
ppt-2.pptx
 
soumay.pptx
soumay.pptxsoumay.pptx
soumay.pptx
 
Data Path Design and Bus Organization.pptx
Data Path Design and Bus Organization.pptxData Path Design and Bus Organization.pptx
Data Path Design and Bus Organization.pptx
 
Python Unit II.pptx
Python Unit II.pptxPython Unit II.pptx
Python Unit II.pptx
 
OOP-Module-1-Section-4-LectureNo1-5.pptx
OOP-Module-1-Section-4-LectureNo1-5.pptxOOP-Module-1-Section-4-LectureNo1-5.pptx
OOP-Module-1-Section-4-LectureNo1-5.pptx
 
Flow Control (1).ppt
Flow Control (1).pptFlow Control (1).ppt
Flow Control (1).ppt
 
BCA Abstraction.pptx
BCA Abstraction.pptxBCA Abstraction.pptx
BCA Abstraction.pptx
 
BCA Final Keyword.pptx
BCA Final Keyword.pptxBCA Final Keyword.pptx
BCA Final Keyword.pptx
 
BCA Super Keyword.pptx
BCA Super Keyword.pptxBCA Super Keyword.pptx
BCA Super Keyword.pptx
 

Recently uploaded

Modelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdfModelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdf
camseq
 
Casting-Defect-inSlab continuous casting.pdf
Casting-Defect-inSlab continuous casting.pdfCasting-Defect-inSlab continuous casting.pdf
Casting-Defect-inSlab continuous casting.pdf
zubairahmad848137
 
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball playEric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
enizeyimana36
 
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
IJNSA Journal
 
New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
wisnuprabawa3
 
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdfIron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
RadiNasr
 
Computational Engineering IITH Presentation
Computational Engineering IITH PresentationComputational Engineering IITH Presentation
Computational Engineering IITH Presentation
co23btech11018
 
Textile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdfTextile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdf
NazakatAliKhoso2
 
CSM Cloud Service Management Presentarion
CSM Cloud Service Management PresentarionCSM Cloud Service Management Presentarion
CSM Cloud Service Management Presentarion
rpskprasana
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
NidhalKahouli2
 
A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...
nooriasukmaningtyas
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
IJECEIAES
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
JamalHussainArman
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
insn4465
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
gerogepatton
 
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
University of Maribor
 
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
Las Vegas Warehouse
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Christina Lin
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
Yasser Mahgoub
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
Hitesh Mohapatra
 

Recently uploaded (20)

Modelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdfModelagem de um CSTR com reação endotermica.pdf
Modelagem de um CSTR com reação endotermica.pdf
 
Casting-Defect-inSlab continuous casting.pdf
Casting-Defect-inSlab continuous casting.pdfCasting-Defect-inSlab continuous casting.pdf
Casting-Defect-inSlab continuous casting.pdf
 
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball playEric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
Eric Nizeyimana's document 2006 from gicumbi to ttc nyamata handball play
 
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
 
New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
 
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdfIron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
 
Computational Engineering IITH Presentation
Computational Engineering IITH PresentationComputational Engineering IITH Presentation
Computational Engineering IITH Presentation
 
Textile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdfTextile Chemical Processing and Dyeing.pdf
Textile Chemical Processing and Dyeing.pdf
 
CSM Cloud Service Management Presentarion
CSM Cloud Service Management PresentarionCSM Cloud Service Management Presentarion
CSM Cloud Service Management Presentarion
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
 
A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
 
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...
 
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have oneISPM 15 Heat Treated Wood Stamps and why your shipping must have one
ISPM 15 Heat Treated Wood Stamps and why your shipping must have one
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
 
Generative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of contentGenerative AI leverages algorithms to create various forms of content
Generative AI leverages algorithms to create various forms of content
 

BCA MultiThreading.ppt

  • 2. Thread • A thread is a lightweight sub-process.
  • 3. Cont… • The smallest unit of processing. • Threads are independent
  • 4. Cont… • If there occurs exception in one thread, it doesn't affect other threads. • It uses a shared memory area.
  • 5. Multithreading • Multithreading in Java is a process of executing multiple threads simultaneously. Advantages of Java Multithreading • You can perform many operations together, so it saves time.
  • 6. Cont… • It doesn't block the user because threads are independent and you can perform multiple operations at the same time. • Threads are independent, so it doesn't affect other threads if an exception occurs in a single thread.
  • 7. Application of Multi Threading • Games • Animations
  • 8. Life cycle of a Thread (Thread States) • There are 5 states in the life cycle of thread . 1. New 2. Runnable 3. Running 4. Non-Runnable (Blocked) 5. Terminated Note: The life cycle of the thread in java is controlled by JVM.
  • 9. Diagram for Life Cycle of Thread run()
  • 10. Cont… 1. New: The thread is in new state if you create an instance (object) of Thread class but before the calling of start() method.
  • 11. Cont… 2. Runnable: The thread is in runnable state after calling of start() method, but the thread scheduler has not selected it to be the running thread.
  • 12. Cont… 3. Running: The thread is in running state if the thread scheduler has selected it and in this stage run() method is called.
  • 13. Cont… 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.
  • 14. How to create thread • There are two ways to create a thread: 1. By extending Thread class 2. By implementing Runnable interface.
  • 15. 1. By extending Thread class Thread class: • Thread class provide constructors and methods to create and perform operations on a thread. • Thread class extends Object class and implements Runnable interface.
  • 16. Constructors of Thread class • Thread() • Thread(String name)
  • 17. Methods of Thread class 1. public void start(): starts the execution of the thread. JVM calls the run() method of the thread. 2. public void run(): is used to perform action for a thread. It is declared in Runnable Interface
  • 18. Cont… 3. public static void sleep(long milis): It sleeps a thread for the specified amount of time. 4. public void final setPriority(int newPriority): It set the priority of the thread. 5. public int getPriority(): It returns the priority of the thread.
  • 19. Example class A extends Thread { public void run() { System.out.println(“Thread is running”); } public static void main(String a[]) { A t1 = new A(); t1.start(); } } Output: Thread is running
  • 20. 2. By implementing Runnable interface. Runnable interface: • It contains only run() Method.
  • 21. Example class A implements Runnable { public void run() { System.out.println(“Runnable Interface”); } public static void main(String a[]) { A t1 = new A(); Thread t2 = new Thread(t1); t2.start(); } } Output: Runnable Interface
  • 22. Priority of a Thread • Each thread has a priority. • Priorities are represented by a number between 1 and 10. • Default priority of a thread is 5
  • 23. Cont… • public final void setPriority(int newPriority): It set the priority of the thread which is 1 (minimum) to 10 (maximum). • 3 constants defined in Thread class: – public static int MIN_PRIORITY = 1 – public static int NORM_PRIORITY = 5 – public static int MAX_PRIORITY = 10
  • 24. Cont… • public int getPriority(): It returns the priority of the thread.
  • 25. Example class A extends Thread { public static void main(String a[]) { A t1= new A(); t1.setPriority(Thread. MAX_PRIORITY); System.out.println(t1.getPriority()); } } Output: 10