SlideShare a Scribd company logo
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Java String
01
02
03
05
06
07
What is a Java Thread?
Thread Lifecycle
Creating a Thread
Main Thread
Multi Threading
Thread Pool
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
What is a Java Thread?
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
What is a Java Thread?
Thread is a lightweight sub process
Contains a separate path of execution
A thread is created & controlled by the java.lang.Thread class
It is the smallest independent unit of a program
Every Java program contains at least one thread
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Java Thread Life-Cycle
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Thread Lifecycle
New
Runnable
Running
Terminated
Waiting
A Java thread can lie only in
one of the shown states at
any point of time
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Thread Lifecycle
New
Runnable
Running
Terminated
Waiting
A new thread begins its life cycle in this state & remains here until the program
starts the thread. It is also known as a born thread.
New
Once a newly born thread starts, the thread comes under runnable state. A thread
stays in this state is until it is executing its task.
Runnable
In this state a thread starts executing by entering run() method and the yield()
method can send them to go back to the Runnable state.
Running
A thread enters this state when it is temporarily in an inactive state i.e it is still
alive but is not eligible to run. It is can be in waiting, sleeping or blocked state.
Waiting
A runnable thread enters the terminated state when it completes its task or
otherwise terminates.
Terminated
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Creating a Thread
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
public interface Runnable
public class Thread
extends Object
implements Runnable
Creating A Thread
A thread in Java can be created using two ways
Runnable InterfaceThread Class
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Runnable InterfaceThread Class
1. Create a Thread class
2. Override run() method
3. Create object of the class
4. Invoke start() method to
execute the custom threads run()
public class MyThread extends Thread {
public void run(){
System.out.println(“Edureka’s Thread…");
}
public static void main(String[] args) {
MyThread obj = new MyThread();
obj.start();
}
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Runnable InterfaceThread Class
1. Create a Thread class implementing
Runnable interface
2. Override run() method
3. Create object of the class
4. Invoke start() method using the
object
public class MyThread implements Runnable {
public void run(){
System.out.println(“Edureka’s Thread…");
}
public static void main(String[] args) {
Thread t = new Thread(new MyThread());
t.start();
}
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Runnable InterfaceThread Class vs
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Runnable InterfaceThread Class vs
✓ Each Thread creates its unique object ✓ Each Thread creates its unique object
✓ More memory consumption ✓ More memory consumption
✓ A class extending Thread class can’t extend any
other class
✓ Along with Runnable a class can implement any
other interface
✓ Thread class is extended only if there is a need
of overriding other methods of it
✓ Runnable is implemented only if there is a need of
special run method
✓ Enables tight coupling ✓ Enables loose coupling
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Java Main Thread
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Java Main Thread
Subtitle for arrow graphic
It is executed whenever a Java program starts
Every program must contain this thread for its execution to take place
Java main Thread is needed because of the following reasons
Main thread is the most important thread of a Java Program
2. It must be the last thread to finish execution i.e when the main thread stops program terminates
1. From this other “child” threads are spawned
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Java Main Thread
Main Thread Other Demon Threads
Child Thread A Child Thread B
Child Thread C
JVM
start() start()
start()start()
start()
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Multi Threading In Java
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Multi – Threading
Multi threading is the ability of a program to run two or more threads concurrently, where each thread can handle
a different task at the same time making optimal use of the available resources
Main Thread Main Thread Main Thread
Main Thread
start() start() start()
switching switching
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Thread Pool
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Task Task Task Task Task
Application
Queue
Thread
Thread
ThreadTask Finished
Thread
AssignmentNew Task
Thread Pool
Java thread pool manages the pool of worker threads and contains a queue that keep the
tasks waiting to get executed
JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
Thread Methods
Creating Multiple
Threads
Joining Threads Thread.sleep()
Inter Thread
Communication
Daemon Thread
Java Threads Tutorial | Multithreading In Java Tutorial | Java Tutorial For Beginners | Edureka

More Related Content

ODP
Multithreading In Java
PDF
PPT
Java And Multithreading
PPT
Final keyword in java
PPTX
Method overloading
PDF
Java 8 Lambda Built-in Functional Interfaces
PPTX
Control flow statements in java
PPTX
This keyword in java
Multithreading In Java
Java And Multithreading
Final keyword in java
Method overloading
Java 8 Lambda Built-in Functional Interfaces
Control flow statements in java
This keyword in java

What's hot (20)

PPSX
Collections - Lists, Sets
PPT
Basic of Multithreading in JAva
PPTX
JRE , JDK and platform independent nature of JAVA
PPTX
Java awt (abstract window toolkit)
PDF
Java IO
PPTX
Java interface
PDF
Lambda Expressions in Java | Java Lambda Tutorial | Java Certification Traini...
PPT
Java interfaces
PDF
Java 9 New Features
PDF
Learn Java with Dr. Rifat Shahriyar
PPTX
Access modifier and inheritance
PPTX
JAVA AWT
PPTX
Strings in Java
PPTX
Multithreading in java
PPTX
Java 8 lambda
PPTX
Java string handling
PPTX
Java 8 streams
PDF
Introduction to RxJS
PPT
PDF
Java 10 New Features
Collections - Lists, Sets
Basic of Multithreading in JAva
JRE , JDK and platform independent nature of JAVA
Java awt (abstract window toolkit)
Java IO
Java interface
Lambda Expressions in Java | Java Lambda Tutorial | Java Certification Traini...
Java interfaces
Java 9 New Features
Learn Java with Dr. Rifat Shahriyar
Access modifier and inheritance
JAVA AWT
Strings in Java
Multithreading in java
Java 8 lambda
Java string handling
Java 8 streams
Introduction to RxJS
Java 10 New Features
Ad

Similar to Java Threads Tutorial | Multithreading In Java Tutorial | Java Tutorial For Beginners | Edureka (20)

PDF
Java unit 12
DOCX
Threadnotes
PPTX
Multithreading programming in java
PDF
CSE 3146 M1- MULTI THREADING USING JAVA .pdf
PDF
Java threads
PPTX
L22 multi-threading-introduction
PPTX
Multithreading in java
PPTX
Module 4 - Part 4 - Multithreaded Programming.pptx
PDF
Multithreading Introduction and Lifecyle of thread
PPTX
Multithreadingppt.pptx
PPTX
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx
PPTX
multithreading,thread and processinjava-210302183809.pptx
PPTX
Object-Oriented-Prog_MultiThreading.pptx
PPTX
Multithreading.pptx
PPT
Chap2 2 1
PPTX
Thread Concept: Multithreading, Creating thread using thread
PPTX
unit3multithreadingppt-copy-180122162204.pptx
PPTX
unit3 Exception Handling multithreadingppt.pptx
PPT
Session 7_MULTITHREADING in java example.ppt
PPTX
Multithreading.pptx
Java unit 12
Threadnotes
Multithreading programming in java
CSE 3146 M1- MULTI THREADING USING JAVA .pdf
Java threads
L22 multi-threading-introduction
Multithreading in java
Module 4 - Part 4 - Multithreaded Programming.pptx
Multithreading Introduction and Lifecyle of thread
Multithreadingppt.pptx
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx
multithreading,thread and processinjava-210302183809.pptx
Object-Oriented-Prog_MultiThreading.pptx
Multithreading.pptx
Chap2 2 1
Thread Concept: Multithreading, Creating thread using thread
unit3multithreadingppt-copy-180122162204.pptx
unit3 Exception Handling multithreadingppt.pptx
Session 7_MULTITHREADING in java example.ppt
Multithreading.pptx
Ad

More from Edureka! (20)

PDF
What to learn during the 21 days Lockdown | Edureka
PDF
Top 10 Dying Programming Languages in 2020 | Edureka
PDF
Top 5 Trending Business Intelligence Tools | Edureka
PDF
Tableau Tutorial for Data Science | Edureka
PDF
Python Programming Tutorial | Edureka
PDF
Top 5 PMP Certifications | Edureka
PDF
Top Maven Interview Questions in 2020 | Edureka
PDF
Linux Mint Tutorial | Edureka
PDF
How to Deploy Java Web App in AWS| Edureka
PDF
Importance of Digital Marketing | Edureka
PDF
RPA in 2020 | Edureka
PDF
Email Notifications in Jenkins | Edureka
PDF
EA Algorithm in Machine Learning | Edureka
PDF
Cognitive AI Tutorial | Edureka
PDF
AWS Cloud Practitioner Tutorial | Edureka
PDF
Blue Prism Top Interview Questions | Edureka
PDF
Big Data on AWS Tutorial | Edureka
PDF
A star algorithm | A* Algorithm in Artificial Intelligence | Edureka
PDF
Kubernetes Installation on Ubuntu | Edureka
PDF
Introduction to DevOps | Edureka
What to learn during the 21 days Lockdown | Edureka
Top 10 Dying Programming Languages in 2020 | Edureka
Top 5 Trending Business Intelligence Tools | Edureka
Tableau Tutorial for Data Science | Edureka
Python Programming Tutorial | Edureka
Top 5 PMP Certifications | Edureka
Top Maven Interview Questions in 2020 | Edureka
Linux Mint Tutorial | Edureka
How to Deploy Java Web App in AWS| Edureka
Importance of Digital Marketing | Edureka
RPA in 2020 | Edureka
Email Notifications in Jenkins | Edureka
EA Algorithm in Machine Learning | Edureka
Cognitive AI Tutorial | Edureka
AWS Cloud Practitioner Tutorial | Edureka
Blue Prism Top Interview Questions | Edureka
Big Data on AWS Tutorial | Edureka
A star algorithm | A* Algorithm in Artificial Intelligence | Edureka
Kubernetes Installation on Ubuntu | Edureka
Introduction to DevOps | Edureka

Recently uploaded (20)

PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
DevOps & Developer Experience Summer BBQ
PDF
GamePlan Trading System Review: Professional Trader's Honest Take
PPTX
Belt and Road Supply Chain Finance Blockchain Solution
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
PPTX
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
PDF
HCSP-Presales-Campus Network Planning and Design V1.0 Training Material-Witho...
PDF
Enable Enterprise-Ready Security on IBM i Systems.pdf
PDF
ai-archetype-understanding-the-personality-of-agentic-ai.pdf
PDF
Top Generative AI Tools for Patent Drafting in 2025.pdf
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Smarter Business Operations Powered by IoT Remote Monitoring
PPTX
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
PPTX
ABU RAUP TUGAS TIK kelas 8 hjhgjhgg.pptx
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
NewMind AI Weekly Chronicles - August'25 Week I
DevOps & Developer Experience Summer BBQ
GamePlan Trading System Review: Professional Trader's Honest Take
Belt and Road Supply Chain Finance Blockchain Solution
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
HCSP-Presales-Campus Network Planning and Design V1.0 Training Material-Witho...
Enable Enterprise-Ready Security on IBM i Systems.pdf
ai-archetype-understanding-the-personality-of-agentic-ai.pdf
Top Generative AI Tools for Patent Drafting in 2025.pdf
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
CIFDAQ's Market Insight: SEC Turns Pro Crypto
NewMind AI Monthly Chronicles - July 2025
Smarter Business Operations Powered by IoT Remote Monitoring
breach-and-attack-simulation-cybersecurity-india-chennai-defenderrabbit-2025....
ABU RAUP TUGAS TIK kelas 8 hjhgjhgg.pptx
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication

Java Threads Tutorial | Multithreading In Java Tutorial | Java Tutorial For Beginners | Edureka

  • 1. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training
  • 2. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Java String 01 02 03 05 06 07 What is a Java Thread? Thread Lifecycle Creating a Thread Main Thread Multi Threading Thread Pool
  • 3. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training What is a Java Thread?
  • 4. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training What is a Java Thread? Thread is a lightweight sub process Contains a separate path of execution A thread is created & controlled by the java.lang.Thread class It is the smallest independent unit of a program Every Java program contains at least one thread
  • 5. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Java Thread Life-Cycle
  • 6. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Thread Lifecycle New Runnable Running Terminated Waiting A Java thread can lie only in one of the shown states at any point of time
  • 7. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Thread Lifecycle New Runnable Running Terminated Waiting A new thread begins its life cycle in this state & remains here until the program starts the thread. It is also known as a born thread. New Once a newly born thread starts, the thread comes under runnable state. A thread stays in this state is until it is executing its task. Runnable In this state a thread starts executing by entering run() method and the yield() method can send them to go back to the Runnable state. Running A thread enters this state when it is temporarily in an inactive state i.e it is still alive but is not eligible to run. It is can be in waiting, sleeping or blocked state. Waiting A runnable thread enters the terminated state when it completes its task or otherwise terminates. Terminated
  • 8. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Creating a Thread
  • 9. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training public interface Runnable public class Thread extends Object implements Runnable Creating A Thread A thread in Java can be created using two ways Runnable InterfaceThread Class
  • 10. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Runnable InterfaceThread Class 1. Create a Thread class 2. Override run() method 3. Create object of the class 4. Invoke start() method to execute the custom threads run() public class MyThread extends Thread { public void run(){ System.out.println(“Edureka’s Thread…"); } public static void main(String[] args) { MyThread obj = new MyThread(); obj.start(); }
  • 11. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Runnable InterfaceThread Class 1. Create a Thread class implementing Runnable interface 2. Override run() method 3. Create object of the class 4. Invoke start() method using the object public class MyThread implements Runnable { public void run(){ System.out.println(“Edureka’s Thread…"); } public static void main(String[] args) { Thread t = new Thread(new MyThread()); t.start(); }
  • 12. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Runnable InterfaceThread Class vs
  • 13. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Runnable InterfaceThread Class vs ✓ Each Thread creates its unique object ✓ Each Thread creates its unique object ✓ More memory consumption ✓ More memory consumption ✓ A class extending Thread class can’t extend any other class ✓ Along with Runnable a class can implement any other interface ✓ Thread class is extended only if there is a need of overriding other methods of it ✓ Runnable is implemented only if there is a need of special run method ✓ Enables tight coupling ✓ Enables loose coupling
  • 14. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Java Main Thread
  • 15. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Java Main Thread Subtitle for arrow graphic It is executed whenever a Java program starts Every program must contain this thread for its execution to take place Java main Thread is needed because of the following reasons Main thread is the most important thread of a Java Program 2. It must be the last thread to finish execution i.e when the main thread stops program terminates 1. From this other “child” threads are spawned
  • 16. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Java Main Thread Main Thread Other Demon Threads Child Thread A Child Thread B Child Thread C JVM start() start() start()start() start()
  • 17. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Multi Threading In Java
  • 18. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Multi – Threading Multi threading is the ability of a program to run two or more threads concurrently, where each thread can handle a different task at the same time making optimal use of the available resources Main Thread Main Thread Main Thread Main Thread start() start() start() switching switching
  • 19. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Thread Pool
  • 20. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Task Task Task Task Task Application Queue Thread Thread ThreadTask Finished Thread AssignmentNew Task Thread Pool Java thread pool manages the pool of worker threads and contains a queue that keep the tasks waiting to get executed
  • 21. JAVA CERTIFICATION TRAINING www.edureka.co/java-j2ee-soa-training Thread Methods Creating Multiple Threads Joining Threads Thread.sleep() Inter Thread Communication Daemon Thread