SlideShare a Scribd company logo
2
Most read
3
Most read
8
Most read
THREADS
Mr.V.M.Prabhakaran,
Department of CSE,
KIT- Coimbatore
INTRODUCTION
• A thread is an independent path of execution
within a program.
• Many threads can run concurrently within a
program.
• Every thread in Java is created and controlled
by the java.lang.Thread class.
• A Java program can have many threads, and
these threads can run concurrently, either
asynchronously or synchronously.
• Thread is a lightweight sub
process
• 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.
• Thread is executed inside the
process. There is context-
switching between the threads.
There can be multiple
processes inside the OS and
one process can have multiple
threads.
Multitasking and Multithreading
• Multitasking:
– refers to a computer's ability to perform multiple jobs
concurrently
– more than one program are running concurrently, e.g., UNIX
• Multithreading:
– A thread is a single sequence of execution within a program
– refers to multiple threads of control within a single program
– each program can run multiple threads of control within it,
e.g., Web Browser
4
Concurrency vs. Parallelism
5
CPU CPU1 CPU2
What are Threads Good For?
• To maintain responsiveness of an application during a
long running task
• To enable cancellation of separable tasks
• Some problems are intrinsically parallel
• To monitor status of some resource (e.g., DB)
• Some APIs and systems demand it (e.g., Swing)
6
Advantage of Java Multithreading
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.
Life cycle of a Thread
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
Java threads
Life cycle of a Thread (Contd)
• New: A new thread begins its life cycle in the new state. It
remains in this state until the program starts the thread. It is also
referred to as a born thread.
• Runnable: After a newly born thread is started, the thread
becomes runnable. A thread in this state is considered to be
executing its task.
• Waiting: Sometimes, a thread transitions to the waiting state
while the thread waits for another thread to perform a task. A
thread transitions back to the runnable state only when another
thread signals the waiting thread to continue executing.
• Timed waiting: A runnable thread can enter the timed waiting
state for a specified interval of time. A thread in this state
transitions back to the runnable state when that time interval
expires or when the event it is waiting for occurs.
• Terminated ( Dead ): A runnable thread enters the terminated
state when it completes its task or otherwise terminates.
Daemon thread
• Daemon thread is a low priority thread (in
context of JVM) that runs in background to
perform tasks such as garbage collection.
• JVM terminates itself when all user threads
(non-daemon threads) finish their execution,
JVM does not care whether Daemon thread is
running or not,
Thread Priority
• When a Java thread is created, it inherits its
priority from the thread that created it.
• You can modify a thread’s priority at any time
after its creation using the setPriority method.
• Thread priorities are integers ranging between
• MIN_PRIORITY (1)
• NORM_PRIORITY (5)
• MAX_PRIORITY (10)
Creating Threads
• There are two ways to create our own Thread
object
– By extending Thread class
– By implementing Runnable interface.
• In both cases the run() method should be
implemented
Commonly used Constructors of
Thread class
• Thread()
• Thread(String name)
• Thread(Runnable r)
• Thread(Runnable r,String name)
Extending Thread
SYNTAX
class Mythread extends Thread
{
public void run(){
--------------
--------------
--------------
--------------
}
}
PROGRAM
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...
Thread Methods
void start()
– Creates a new thread and makes it runnable
– This method can be called only once
void run()
– The new thread begins its life inside this method
void stop() (deprecated)
– The thread is being terminated
Thread Methods
void yield()
– Causes the currently executing thread object to
temporarily pause and allow other threads to
execute
– Allow only threads of the same priority to run
void sleep(int m) or sleep(int m, int n)
– The thread sleeps for m milliseconds, plus n
nanoseconds
Thread Methods
• public void start()
• public void run()
• public final void
setName(String name)
• public final void
setPriority(int priority)
• public final void
setDaemon(boolean on)
• public final void join(long
millisec)
• public void interrupt()
• public final boolean
isAlive()
• public static void yield()
• public static void sleep(long
millisec)
• public static boolean
holdsLock(Object x)
• public static Thread
currentThread()
• public static void
dumpStack()
Implementing Runnable
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...

More Related Content

What's hot (20)

PPTX
Multithreading in java
Monika Mishra
 
PPTX
Multithreading in java
Arafat Hossan
 
PPT
Java interfaces
Raja Sekhar
 
PPT
Java And Multithreading
Shraddha
 
PPTX
java interface and packages
VINOTH R
 
PPTX
Applets in java
Wani Zahoor
 
PPS
Java rmi
kamal kotecha
 
PPT
Input output streams
Parthipan Parthi
 
PPT
Exception Handling in JAVA
SURIT DATTA
 
PDF
Methods in Java
Jussi Pohjolainen
 
PPTX
Java string handling
Salman Khan
 
PPTX
Exception Handling in Java
lalithambiga kamaraj
 
PPTX
Java abstract class & abstract methods
Shubham Dwivedi
 
PPT
Java buzzwords
ramesh517
 
PPTX
Constructor in java
Pavith Gunasekara
 
PPTX
Super Keyword in Java.pptx
KrutikaWankhade1
 
PPT
Java collections concept
kumar gaurav
 
PPTX
I/O Streams
Ravi Chythanya
 
PPTX
Type casting in java
Farooq Baloch
 
Multithreading in java
Monika Mishra
 
Multithreading in java
Arafat Hossan
 
Java interfaces
Raja Sekhar
 
Java And Multithreading
Shraddha
 
java interface and packages
VINOTH R
 
Applets in java
Wani Zahoor
 
Java rmi
kamal kotecha
 
Input output streams
Parthipan Parthi
 
Exception Handling in JAVA
SURIT DATTA
 
Methods in Java
Jussi Pohjolainen
 
Java string handling
Salman Khan
 
Exception Handling in Java
lalithambiga kamaraj
 
Java abstract class & abstract methods
Shubham Dwivedi
 
Java buzzwords
ramesh517
 
Constructor in java
Pavith Gunasekara
 
Super Keyword in Java.pptx
KrutikaWankhade1
 
Java collections concept
kumar gaurav
 
I/O Streams
Ravi Chythanya
 
Type casting in java
Farooq Baloch
 

Similar to Java threads (20)

PPTX
Multithreading in java
junnubabu
 
PPTX
multithreading,thread and processinjava-210302183809.pptx
ArunPatrick2
 
PDF
Unit-3 MULTITHREADING-2.pdf
GouthamSoma1
 
PPTX
Multi-Threading in Java power point presenetation
AshokRachapalli1
 
PDF
JAVA 3.2.pdfhdfkjhdfvbjdbjfhjdfhdjhfjdfdjfhdjhjd
KusumitaSahoo1
 
PPTX
unit3multithreadingppt-copy-180122162204.pptx
ArunPatrick2
 
PPTX
unit3 Exception Handling multithreadingppt.pptx
ArunPatrick2
 
PDF
Java unit 12
Shipra Swati
 
PPTX
Internet Programming with Java
kavitha muneeshwaran
 
PPTX
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx
sandhyakiran10
 
PPTX
Module 4 - Part 4 - Multithreaded Programming.pptx
FahmaFamzin
 
PPTX
Multithreading in java
Raghu nath
 
PPTX
Multithreading programming in java
Elizabeth alexander
 
PPT
Java
mdfkhan625
 
PPTX
Object-Oriented-Prog_MultiThreading.pptx
NasreenTaj20
 
PPT
multithreading
Rajkattamuri
 
PPTX
Multithreading in Java Object Oriented Programming language
arnavytstudio2814
 
PPT
java multi threading and synchronisation.ppt
ansariparveen06
 
PPT
Java Multithreading
Rajkattamuri
 
Multithreading in java
junnubabu
 
multithreading,thread and processinjava-210302183809.pptx
ArunPatrick2
 
Unit-3 MULTITHREADING-2.pdf
GouthamSoma1
 
Multi-Threading in Java power point presenetation
AshokRachapalli1
 
JAVA 3.2.pdfhdfkjhdfvbjdbjfhjdfhdjhfjdfdjfhdjhjd
KusumitaSahoo1
 
unit3multithreadingppt-copy-180122162204.pptx
ArunPatrick2
 
unit3 Exception Handling multithreadingppt.pptx
ArunPatrick2
 
Java unit 12
Shipra Swati
 
Internet Programming with Java
kavitha muneeshwaran
 
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx
sandhyakiran10
 
Module 4 - Part 4 - Multithreaded Programming.pptx
FahmaFamzin
 
Multithreading in java
Raghu nath
 
Multithreading programming in java
Elizabeth alexander
 
Object-Oriented-Prog_MultiThreading.pptx
NasreenTaj20
 
multithreading
Rajkattamuri
 
Multithreading in Java Object Oriented Programming language
arnavytstudio2814
 
java multi threading and synchronisation.ppt
ansariparveen06
 
Java Multithreading
Rajkattamuri
 
Ad

More from Prabhakaran V M (8)

PDF
Strings in python
Prabhakaran V M
 
PDF
Operators in python
Prabhakaran V M
 
PDF
Algorithmic problem solving
Prabhakaran V M
 
PDF
Open mp directives
Prabhakaran V M
 
PDF
Xml schema
Prabhakaran V M
 
PDF
Html 5
Prabhakaran V M
 
PDF
Introduction to Multi-core Architectures
Prabhakaran V M
 
PDF
Applets
Prabhakaran V M
 
Strings in python
Prabhakaran V M
 
Operators in python
Prabhakaran V M
 
Algorithmic problem solving
Prabhakaran V M
 
Open mp directives
Prabhakaran V M
 
Xml schema
Prabhakaran V M
 
Introduction to Multi-core Architectures
Prabhakaran V M
 
Ad

Recently uploaded (20)

PPTX
MiniTool Power Data Recovery Full Crack Latest 2025
muhammadgurbazkhan
 
PDF
Transparency into Your Software’s True Reach
team-WIBU
 
PDF
From Chaos to Clarity: Mastering Analytics Governance in the Modern Enterprise
Wiiisdom
 
PDF
Power BI vs Tableau vs Looker - Which BI Tool is Right for You?
MagnusMinds IT Solution LLP
 
PDF
Rewards and Recognition (2).pdf
ethan Talor
 
PPTX
3uTools Full Crack Free Version Download [Latest] 2025
muhammadgurbazkhan
 
PDF
Difference Between Kubernetes and Docker .pdf
Kindlebit Solutions
 
PPTX
ManageIQ - Sprint 265 Review - Slide Deck
ManageIQ
 
PDF
LPS25 - Operationalizing MLOps in GEP - Terradue.pdf
terradue
 
PDF
65811_Introducing the Fusion AI Agent Studio (1).pdf
g6129590
 
PPTX
computer forensics encase emager app exp6 1.pptx
ssuser343e92
 
PDF
The Rise of Sustainable Mobile App Solutions by New York Development Firms
ostechnologies16
 
PPTX
Android Notifications-A Guide to User-Facing Alerts in Android .pptx
Nabin Dhakal
 
PDF
>Nitro Pro Crack 14.36.1.0 + Keygen Free Download [Latest]
utfefguu
 
PPTX
ERP - FICO Presentation BY BSL BOKARO STEEL LIMITED.pptx
ravisranjan
 
PDF
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 
PDF
>Wondershare Filmora Crack Free Download 2025
utfefguu
 
PPTX
Quality on Autopilot: Scaling Testing in Uyuni
Oscar Barrios Torrero
 
PDF
Understanding the Need for Systemic Change in Open Source Through Intersectio...
Imma Valls Bernaus
 
PPTX
CONCEPT OF PROGRAMMING in language .pptx
tamim41
 
MiniTool Power Data Recovery Full Crack Latest 2025
muhammadgurbazkhan
 
Transparency into Your Software’s True Reach
team-WIBU
 
From Chaos to Clarity: Mastering Analytics Governance in the Modern Enterprise
Wiiisdom
 
Power BI vs Tableau vs Looker - Which BI Tool is Right for You?
MagnusMinds IT Solution LLP
 
Rewards and Recognition (2).pdf
ethan Talor
 
3uTools Full Crack Free Version Download [Latest] 2025
muhammadgurbazkhan
 
Difference Between Kubernetes and Docker .pdf
Kindlebit Solutions
 
ManageIQ - Sprint 265 Review - Slide Deck
ManageIQ
 
LPS25 - Operationalizing MLOps in GEP - Terradue.pdf
terradue
 
65811_Introducing the Fusion AI Agent Studio (1).pdf
g6129590
 
computer forensics encase emager app exp6 1.pptx
ssuser343e92
 
The Rise of Sustainable Mobile App Solutions by New York Development Firms
ostechnologies16
 
Android Notifications-A Guide to User-Facing Alerts in Android .pptx
Nabin Dhakal
 
>Nitro Pro Crack 14.36.1.0 + Keygen Free Download [Latest]
utfefguu
 
ERP - FICO Presentation BY BSL BOKARO STEEL LIMITED.pptx
ravisranjan
 
Powering GIS with FME and VertiGIS - Peak of Data & AI 2025
Safe Software
 
>Wondershare Filmora Crack Free Download 2025
utfefguu
 
Quality on Autopilot: Scaling Testing in Uyuni
Oscar Barrios Torrero
 
Understanding the Need for Systemic Change in Open Source Through Intersectio...
Imma Valls Bernaus
 
CONCEPT OF PROGRAMMING in language .pptx
tamim41
 

Java threads

  • 2. INTRODUCTION • A thread is an independent path of execution within a program. • Many threads can run concurrently within a program. • Every thread in Java is created and controlled by the java.lang.Thread class. • A Java program can have many threads, and these threads can run concurrently, either asynchronously or synchronously.
  • 3. • Thread is a lightweight sub process • 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. • Thread is executed inside the process. There is context- switching between the threads. There can be multiple processes inside the OS and one process can have multiple threads.
  • 4. Multitasking and Multithreading • Multitasking: – refers to a computer's ability to perform multiple jobs concurrently – more than one program are running concurrently, e.g., UNIX • Multithreading: – A thread is a single sequence of execution within a program – refers to multiple threads of control within a single program – each program can run multiple threads of control within it, e.g., Web Browser 4
  • 6. What are Threads Good For? • To maintain responsiveness of an application during a long running task • To enable cancellation of separable tasks • Some problems are intrinsically parallel • To monitor status of some resource (e.g., DB) • Some APIs and systems demand it (e.g., Swing) 6
  • 7. Advantage of Java Multithreading 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.
  • 8. Life cycle of a Thread 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
  • 10. Life cycle of a Thread (Contd) • New: A new thread begins its life cycle in the new state. It remains in this state until the program starts the thread. It is also referred to as a born thread. • Runnable: After a newly born thread is started, the thread becomes runnable. A thread in this state is considered to be executing its task. • Waiting: Sometimes, a thread transitions to the waiting state while the thread waits for another thread to perform a task. A thread transitions back to the runnable state only when another thread signals the waiting thread to continue executing. • Timed waiting: A runnable thread can enter the timed waiting state for a specified interval of time. A thread in this state transitions back to the runnable state when that time interval expires or when the event it is waiting for occurs. • Terminated ( Dead ): A runnable thread enters the terminated state when it completes its task or otherwise terminates.
  • 11. Daemon thread • Daemon thread is a low priority thread (in context of JVM) that runs in background to perform tasks such as garbage collection. • JVM terminates itself when all user threads (non-daemon threads) finish their execution, JVM does not care whether Daemon thread is running or not,
  • 12. Thread Priority • When a Java thread is created, it inherits its priority from the thread that created it. • You can modify a thread’s priority at any time after its creation using the setPriority method. • Thread priorities are integers ranging between • MIN_PRIORITY (1) • NORM_PRIORITY (5) • MAX_PRIORITY (10)
  • 13. Creating Threads • There are two ways to create our own Thread object – By extending Thread class – By implementing Runnable interface. • In both cases the run() method should be implemented
  • 14. Commonly used Constructors of Thread class • Thread() • Thread(String name) • Thread(Runnable r) • Thread(Runnable r,String name)
  • 15. Extending Thread SYNTAX class Mythread extends Thread { public void run(){ -------------- -------------- -------------- -------------- } } PROGRAM 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...
  • 16. Thread Methods void start() – Creates a new thread and makes it runnable – This method can be called only once void run() – The new thread begins its life inside this method void stop() (deprecated) – The thread is being terminated
  • 17. Thread Methods void yield() – Causes the currently executing thread object to temporarily pause and allow other threads to execute – Allow only threads of the same priority to run void sleep(int m) or sleep(int m, int n) – The thread sleeps for m milliseconds, plus n nanoseconds
  • 18. Thread Methods • public void start() • public void run() • public final void setName(String name) • public final void setPriority(int priority) • public final void setDaemon(boolean on) • public final void join(long millisec) • public void interrupt() • public final boolean isAlive() • public static void yield() • public static void sleep(long millisec) • public static boolean holdsLock(Object x) • public static Thread currentThread() • public static void dumpStack()
  • 19. Implementing Runnable 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...