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

PPT
Java thread
PPT
Thread model in java
PPT
Java Threads
PPTX
Threads in Java
PDF
Threads concept in java
PDF
Java threads
PPT
Java And Multithreading
PPTX
Java Multi Thead Programming
Java thread
Thread model in java
Java Threads
Threads in Java
Threads concept in java
Java threads
Java And Multithreading
Java Multi Thead Programming

What's hot (20)

PDF
javathreads
PPTX
PPTX
Thread presentation
PPTX
Thread model of java
PDF
Java threading
PPTX
PPTX
Multithread
PPT
Threads in Java
PPT
Java Threads and Concurrency
PDF
Java Thread Synchronization
PPT
Learning Java 3 – Threads and Synchronization
PPTX
Multithreading in java
PPTX
Java Thread & Multithreading
PPTX
Multithread Programing in Java
PPT
Thread
PPTX
PDF
Java unit 12
ODP
Multithreading In Java
PPTX
Multithreading in java
javathreads
Thread presentation
Thread model of java
Java threading
Multithread
Threads in Java
Java Threads and Concurrency
Java Thread Synchronization
Learning Java 3 – Threads and Synchronization
Multithreading in java
Java Thread & Multithreading
Multithread Programing in Java
Thread
Java unit 12
Multithreading In Java
Multithreading in java
Ad

Similar to Thread&multithread (20)

PDF
Class notes(week 9) on multithreading
PPTX
Concept of Java Multithreading-Partially.pptx
PPTX
Multithreading in Java Object Oriented Programming language
DOCX
Module - 5 merged.docx notes about engineering subjects java
PPTX
Multi threading
PPTX
OOPS object oriented programming UNIT-4.pptx
PPT
Chap2 2 1
PPTX
Internet Programming with Java
PPT
BCA MultiThreading.ppt
PPTX
Multi-Threading in Java power point presenetation
PPTX
Object-Oriented-Prog_MultiThreading.pptx
PPT
web programming-Multithreading concept in Java.ppt
PPT
multithreading, creating a thread and life cycle in java.ppt
PPTX
multithreading,thread and processinjava-210302183809.pptx
PDF
Multithreading Introduction and Lifecyle of thread
PPTX
Multithreading programming in java
PPTX
L22 multi-threading-introduction
PDF
Multi threading
PDF
Unit 5 - Java Multihhhhhhhhhhhhhhhhhhhhaaaaaaaaaaaaaaaaathreading.pdf
Class notes(week 9) on multithreading
Concept of Java Multithreading-Partially.pptx
Multithreading in Java Object Oriented Programming language
Module - 5 merged.docx notes about engineering subjects java
Multi threading
OOPS object oriented programming UNIT-4.pptx
Chap2 2 1
Internet Programming with Java
BCA MultiThreading.ppt
Multi-Threading in Java power point presenetation
Object-Oriented-Prog_MultiThreading.pptx
web programming-Multithreading concept in Java.ppt
multithreading, creating a thread and life cycle in java.ppt
multithreading,thread and processinjava-210302183809.pptx
Multithreading Introduction and Lifecyle of thread
Multithreading programming in java
L22 multi-threading-introduction
Multi threading
Unit 5 - Java Multihhhhhhhhhhhhhhhhhhhhaaaaaaaaaaaaaaaaathreading.pdf
Ad

More from PhD Research Scholar (20)

PPTX
Quiz servlet
PPTX
servlet db connectivity
PPTX
2.java script dom
PPTX
1.java script
PPTX
Quiz javascript
PPTX
Streams&io
PPTX
PPTX
Interface in java
PPTX
Inner classes in java
PPTX
PPTX
Exception handling
PPTX
Abstract class
PPTX
7. tuples, set & dictionary
PPTX
PPTX
4. functions
Quiz servlet
servlet db connectivity
2.java script dom
1.java script
Quiz javascript
Streams&io
Interface in java
Inner classes in java
Exception handling
Abstract class
7. tuples, set & dictionary
4. functions

Recently uploaded (20)

PDF
Business Ethics Teaching Materials for college
PDF
RMMM.pdf make it easy to upload and study
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Cell Types and Its function , kingdom of life
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
01-Introduction-to-Information-Management.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Business Ethics Teaching Materials for college
RMMM.pdf make it easy to upload and study
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Renaissance Architecture: A Journey from Faith to Humanism
human mycosis Human fungal infections are called human mycosis..pptx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Supply Chain Operations Speaking Notes -ICLT Program
PPH.pptx obstetrics and gynecology in nursing
STATICS OF THE RIGID BODIES Hibbelers.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
VCE English Exam - Section C Student Revision Booklet
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
Week 4 Term 3 Study Techniques revisited.pptx
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Cell Types and Its function , kingdom of life
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
01-Introduction-to-Information-Management.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
The Lost Whites of Pakistan by Jahanzaib Mughal.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.