SlideShare a Scribd company logo
THREADS
EXPLAINED
In 45 minutes
ALL MATERIAL PUBLISHED UNDER GNU GPL V4 OR LATER.
What is a Thread?
A thread of
execution is the
smallest
sequence of
programmed
instructions
that are
managed by a
process.
A process can have
multiple threads
whereas vice versa is
not true.
Thread States
A thread state. A thread can be in one of the following states:
NEW: A thread that has not yet started is in this state.
RUNNABLE: A thread executing in the Java virtual machine is in this state.
BLOCKED: A thread that is blocked waiting for a monitor lock is in this state.
WAITING :A thread that is waiting indefinitely for another thread to perform
a particular action is in this state.
TIMED_WAITING: A thread that is waiting for another thread to perform an
action for up to a specified waiting time is in this state.
TERMINATED: A thread that has exited is in this state.
A thread can be in only one state at a given point in time.
These states are virtual machine states which
do not reflect any operating system thread
states.
Hello World program uses Threads.
When the JVM starts, it creates a thread called "Main". Your program will
run on this thread, unless you create additional threads yourself.
The first thing the "Main" thread does is to look for your static void
main(String[] argv) method and invoke it. That is the entry-point to your
program.
If you want things to happen "at the same time", you can create multiple
threads, and give each something to execute. They will then continue to do
these things concurrently. The JVM also creates some internal threads for
background work such as garbage collection.
HelloWorldacloserlook
public class hello{
public static void main(String args[]) {
Thread t = Thread.currentThread();
System.out.println("Hello World"+ t);
}
}
Ways to create a Thread.
1 : implement the Runnable
class.
2 : extend the Thread class.
Example posted at URL:
https://github.com/Pradhan10/threads-
explained/tree/master
Pop Quiz
Why did the second example using extnds Thread class not
print child thread name ?
Take a closer look 
/* This example illustrates creation of thread by extending Thread class.
* Author : Pradhan Rishi Sharma
* Email : pradhanrishi10@gmail.com
*/
public class HelloThread extends Thread {
public void run() {
Thread child = Thread.currentThread();
child.setName("Child");
System.out.println("Hello from a thread!");
}
public static void main(String args[]) {
Thread main = Thread.currentThread();
System.out.println("Hello from main"+" "+main);
(new HelloThread()).start();
}
}
Can you spot the error in code? 
Can anyone guess a problem with
threads ? 
If not , the presentation is over ! 
Example problem URL
: https://github.com/Pradhan10/threads-
explained/blob/master/TestThread.java
JAVA Threads explained
That's all for today folks ! 

More Related Content

What's hot (20)

PPT
MULTITHREADING CONCEPT
RAVI MAURYA
 
PPT
JavaScript iteration
Charles Russell
 
PDF
Java thread life cycle
Archana Gopinath
 
PPT
testing ppt
techweb08
 
PPTX
Understanding concurrency
Anshul Sharma
 
PPTX
Introduction to Multithreading in Java
Rakesh Mittal
 
PDF
22 multi threading iv
Ravindra Rathore
 
PPT
Get ready for FRC 2015: Intro to Java 5 through 8 updates and Eclipse
Jeanne Boyarsky
 
PDF
Concurrency
Isaac Liao
 
PPTX
Phasers to stunning
Simon Proctor
 
PPTX
Bsides tampa
Octavio Paguaga
 
PPT
Oop lecture2
Shahriar Robbani
 
PPTX
Intro to Node.js (v1)
Chris Cowan
 
PPTX
Threads .ppt
meet darji
 
PPTX
Node js Global Packages
sanskriti agarwal
 
PDF
Puppet@Citygrid - Julien Rottenberg - PuppetCamp LA '12
Puppet
 
PPTX
TDD a piccoli passi
Ferdinando Santacroce
 
PDF
An Introduction of Node Package Manager (NPM)
iFour Technolab Pvt. Ltd.
 
MULTITHREADING CONCEPT
RAVI MAURYA
 
JavaScript iteration
Charles Russell
 
Java thread life cycle
Archana Gopinath
 
testing ppt
techweb08
 
Understanding concurrency
Anshul Sharma
 
Introduction to Multithreading in Java
Rakesh Mittal
 
22 multi threading iv
Ravindra Rathore
 
Get ready for FRC 2015: Intro to Java 5 through 8 updates and Eclipse
Jeanne Boyarsky
 
Concurrency
Isaac Liao
 
Phasers to stunning
Simon Proctor
 
Bsides tampa
Octavio Paguaga
 
Oop lecture2
Shahriar Robbani
 
Intro to Node.js (v1)
Chris Cowan
 
Threads .ppt
meet darji
 
Node js Global Packages
sanskriti agarwal
 
Puppet@Citygrid - Julien Rottenberg - PuppetCamp LA '12
Puppet
 
TDD a piccoli passi
Ferdinando Santacroce
 
An Introduction of Node Package Manager (NPM)
iFour Technolab Pvt. Ltd.
 

Similar to JAVA Threads explained (20)

PDF
Java Threads: Lightweight Processes
Isuru Perera
 
PPTX
Multi-Threading in Java power point presenetation
AshokRachapalli1
 
DOCX
Threadnotes
Himanshu Rajput
 
PPTX
multithreading,thread and processinjava-210302183809.pptx
ArunPatrick2
 
PPTX
Multithreading in java
junnubabu
 
PPT
java multi threading and synchronisation.ppt
ansariparveen06
 
PPTX
Multithreading in java
Raghu nath
 
PPTX
Thread Concept: Multithreading, Creating thread using thread
poongothai11
 
PPT
JAVA MULTITHREDED PROGRAMMING - LECTURES
rm170484
 
PDF
Java threads
Prabhakaran V M
 
PDF
JAVA 3.2.pdfhdfkjhdfvbjdbjfhjdfhdjhfjdfdjfhdjhjd
KusumitaSahoo1
 
PDF
Multithreading Introduction and Lifecyle of thread
Kartik Dube
 
PPTX
multithreading to be used in java with good programs.pptx
PriyadharshiniG41
 
PDF
Java unit 12
Shipra Swati
 
PPTX
Multithreading in Java Object Oriented Programming language
arnavytstudio2814
 
PPTX
Multithreading
SanthiNivas
 
PPTX
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx
sandhyakiran10
 
PPTX
Thread presentation
AAshish Ojha
 
PPTX
Thread Concept.pptx
kvigneshjp
 
PPTX
OOPS object oriented programming UNIT-4.pptx
Arulmozhivarman8
 
Java Threads: Lightweight Processes
Isuru Perera
 
Multi-Threading in Java power point presenetation
AshokRachapalli1
 
Threadnotes
Himanshu Rajput
 
multithreading,thread and processinjava-210302183809.pptx
ArunPatrick2
 
Multithreading in java
junnubabu
 
java multi threading and synchronisation.ppt
ansariparveen06
 
Multithreading in java
Raghu nath
 
Thread Concept: Multithreading, Creating thread using thread
poongothai11
 
JAVA MULTITHREDED PROGRAMMING - LECTURES
rm170484
 
Java threads
Prabhakaran V M
 
JAVA 3.2.pdfhdfkjhdfvbjdbjfhjdfhdjhfjdfdjfhdjhjd
KusumitaSahoo1
 
Multithreading Introduction and Lifecyle of thread
Kartik Dube
 
multithreading to be used in java with good programs.pptx
PriyadharshiniG41
 
Java unit 12
Shipra Swati
 
Multithreading in Java Object Oriented Programming language
arnavytstudio2814
 
Multithreading
SanthiNivas
 
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx
sandhyakiran10
 
Thread presentation
AAshish Ojha
 
Thread Concept.pptx
kvigneshjp
 
OOPS object oriented programming UNIT-4.pptx
Arulmozhivarman8
 
Ad

More from Pradhan Rishi Sharma (20)

PDF
JAVA Collection and generics
Pradhan Rishi Sharma
 
PDF
Plasma TFT
Pradhan Rishi Sharma
 
ODP
Inheritance
Pradhan Rishi Sharma
 
PDF
Inheritance
Pradhan Rishi Sharma
 
PDF
0/1Knapsack
Pradhan Rishi Sharma
 
PDF
Computer system architecture
Pradhan Rishi Sharma
 
PPTX
Aids Awareness , Nss , Healthcare !
Pradhan Rishi Sharma
 
ODP
D Flip Flop
Pradhan Rishi Sharma
 
ODP
Optical fiber
Pradhan Rishi Sharma
 
PPT
Optical Fiber
Pradhan Rishi Sharma
 
PPTX
Algorithms
Pradhan Rishi Sharma
 
PPT
Algorithms
Pradhan Rishi Sharma
 
ODP
Algorithms
Pradhan Rishi Sharma
 
ODP
Algorithms
Pradhan Rishi Sharma
 
PPTX
Solar Energy
Pradhan Rishi Sharma
 
PDF
Solar Energy
Pradhan Rishi Sharma
 
ODP
Solar Energy
Pradhan Rishi Sharma
 
PPTX
Beam , support and reaction
Pradhan Rishi Sharma
 
ODP
Beam , support and reaction
Pradhan Rishi Sharma
 
JAVA Collection and generics
Pradhan Rishi Sharma
 
Computer system architecture
Pradhan Rishi Sharma
 
Aids Awareness , Nss , Healthcare !
Pradhan Rishi Sharma
 
D Flip Flop
Pradhan Rishi Sharma
 
Optical fiber
Pradhan Rishi Sharma
 
Optical Fiber
Pradhan Rishi Sharma
 
Solar Energy
Pradhan Rishi Sharma
 
Solar Energy
Pradhan Rishi Sharma
 
Solar Energy
Pradhan Rishi Sharma
 
Beam , support and reaction
Pradhan Rishi Sharma
 
Beam , support and reaction
Pradhan Rishi Sharma
 
Ad

Recently uploaded (20)

PDF
Proactive Server and System Monitoring with FME: Using HTTP and System Caller...
Safe Software
 
PPSX
Usergroup - OutSystems Architecture.ppsx
Kurt Vandevelde
 
PDF
Bridging CAD, IBM TRIRIGA & GIS with FME: The Portland Public Schools Case
Safe Software
 
PDF
Automating the Geo-Referencing of Historic Aerial Photography in Flanders
Safe Software
 
PDF
Why aren't you using FME Flow's CPU Time?
Safe Software
 
PDF
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
 
PDF
The Future of Product Management in AI ERA.pdf
Alyona Owens
 
PPTX
The birth and death of Stars - earth and life science
rizellemarieastrolo
 
PDF
My Journey from CAD to BIM: A True Underdog Story
Safe Software
 
PDF
Java 25 and Beyond - A Roadmap of Innovations
Ana-Maria Mihalceanu
 
PDF
Next level data operations using Power Automate magic
Andries den Haan
 
PDF
“Scaling i.MX Applications Processors’ Native Edge AI with Discrete AI Accele...
Edge AI and Vision Alliance
 
PPTX
01_Approach Cyber- DORA Incident Management.pptx
FinTech Belgium
 
PPTX
Mastering Authorization: Integrating Authentication and Authorization Data in...
Hitachi, Ltd. OSS Solution Center.
 
PDF
LLM Search Readiness Audit - Dentsu x SEO Square - June 2025.pdf
Nick Samuel
 
PDF
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
DOCX
Daily Lesson Log MATATAG ICT TEchnology 8
LOIDAALMAZAN3
 
PDF
Redefining Work in the Age of AI - What to expect? How to prepare? Why it mat...
Malinda Kapuruge
 
PPTX
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Poster...
Michele Kryston
 
Proactive Server and System Monitoring with FME: Using HTTP and System Caller...
Safe Software
 
Usergroup - OutSystems Architecture.ppsx
Kurt Vandevelde
 
Bridging CAD, IBM TRIRIGA & GIS with FME: The Portland Public Schools Case
Safe Software
 
Automating the Geo-Referencing of Historic Aerial Photography in Flanders
Safe Software
 
Why aren't you using FME Flow's CPU Time?
Safe Software
 
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
 
The Future of Product Management in AI ERA.pdf
Alyona Owens
 
The birth and death of Stars - earth and life science
rizellemarieastrolo
 
My Journey from CAD to BIM: A True Underdog Story
Safe Software
 
Java 25 and Beyond - A Roadmap of Innovations
Ana-Maria Mihalceanu
 
Next level data operations using Power Automate magic
Andries den Haan
 
“Scaling i.MX Applications Processors’ Native Edge AI with Discrete AI Accele...
Edge AI and Vision Alliance
 
01_Approach Cyber- DORA Incident Management.pptx
FinTech Belgium
 
Mastering Authorization: Integrating Authentication and Authorization Data in...
Hitachi, Ltd. OSS Solution Center.
 
LLM Search Readiness Audit - Dentsu x SEO Square - June 2025.pdf
Nick Samuel
 
Hyderabad MuleSoft In-Person Meetup (June 21, 2025) Slides
Ravi Tamada
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Daily Lesson Log MATATAG ICT TEchnology 8
LOIDAALMAZAN3
 
Redefining Work in the Age of AI - What to expect? How to prepare? Why it mat...
Malinda Kapuruge
 
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Poster...
Michele Kryston
 

JAVA Threads explained

  • 1. THREADS EXPLAINED In 45 minutes ALL MATERIAL PUBLISHED UNDER GNU GPL V4 OR LATER.
  • 2. What is a Thread? A thread of execution is the smallest sequence of programmed instructions that are managed by a process. A process can have multiple threads whereas vice versa is not true.
  • 3. Thread States A thread state. A thread can be in one of the following states: NEW: A thread that has not yet started is in this state. RUNNABLE: A thread executing in the Java virtual machine is in this state. BLOCKED: A thread that is blocked waiting for a monitor lock is in this state. WAITING :A thread that is waiting indefinitely for another thread to perform a particular action is in this state. TIMED_WAITING: A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state. TERMINATED: A thread that has exited is in this state. A thread can be in only one state at a given point in time. These states are virtual machine states which do not reflect any operating system thread states.
  • 4. Hello World program uses Threads. When the JVM starts, it creates a thread called "Main". Your program will run on this thread, unless you create additional threads yourself. The first thing the "Main" thread does is to look for your static void main(String[] argv) method and invoke it. That is the entry-point to your program. If you want things to happen "at the same time", you can create multiple threads, and give each something to execute. They will then continue to do these things concurrently. The JVM also creates some internal threads for background work such as garbage collection.
  • 5. HelloWorldacloserlook public class hello{ public static void main(String args[]) { Thread t = Thread.currentThread(); System.out.println("Hello World"+ t); } }
  • 6. Ways to create a Thread. 1 : implement the Runnable class. 2 : extend the Thread class. Example posted at URL: https://github.com/Pradhan10/threads- explained/tree/master
  • 7. Pop Quiz Why did the second example using extnds Thread class not print child thread name ?
  • 8. Take a closer look  /* This example illustrates creation of thread by extending Thread class. * Author : Pradhan Rishi Sharma * Email : [email protected] */ public class HelloThread extends Thread { public void run() { Thread child = Thread.currentThread(); child.setName("Child"); System.out.println("Hello from a thread!"); } public static void main(String args[]) { Thread main = Thread.currentThread(); System.out.println("Hello from main"+" "+main); (new HelloThread()).start(); } }
  • 9. Can you spot the error in code? 
  • 10. Can anyone guess a problem with threads ?  If not , the presentation is over !  Example problem URL : https://github.com/Pradhan10/threads- explained/blob/master/TestThread.java
  • 12. That's all for today folks !