SlideShare a Scribd company logo
1 of 20
- 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 (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 multithreadingKuntal Bhowmick
 
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
 
Java-Threads And Concurrency Presentation. 2024
Java-Threads And Concurrency Presentation. 2024Java-Threads And Concurrency Presentation. 2024
Java-Threads And Concurrency Presentation. 2024nehakumari0xf
 
Java Threads And Concurrency Presentation. 2024
Java Threads And Concurrency Presentation. 2024Java Threads And Concurrency Presentation. 2024
Java Threads And Concurrency Presentation. 2024kashyapneha2809
 
BCA MultiThreading.ppt
BCA MultiThreading.pptBCA MultiThreading.ppt
BCA MultiThreading.pptsarthakgithub
 
Multi-threaded Programming in JAVA
Multi-threaded Programming in JAVAMulti-threaded Programming in JAVA
Multi-threaded Programming in JAVAVikram Kalyani
 
Unit-3 MULTITHREADING-2.pdf
Unit-3 MULTITHREADING-2.pdfUnit-3 MULTITHREADING-2.pdf
Unit-3 MULTITHREADING-2.pdfGouthamSoma1
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaArafat Hossan
 
Multithreading programming in java
Multithreading programming in javaMultithreading programming in java
Multithreading programming in javaElizabeth 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.pptxPavanKumarPNVS
 
Multithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of threadMultithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of threadKartik Dube
 
Class notes(week 9) on multithreading
Class notes(week 9) on multithreadingClass notes(week 9) on multithreading
Class notes(week 9) on multithreadingKuntal Bhowmick
 
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptxnimbalkarvikram966
 

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
 
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
 
Class notes(week 9) on multithreading
Class notes(week 9) on multithreadingClass notes(week 9) on multithreading
Class notes(week 9) on multithreading
 
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
07. Parbdhdjdjdjsjsjdjjdjdjjkdkkdkdkt.pptx
 

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

Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 

Recently uploaded (20)

Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 

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.