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

What's hot (20)

cours-gratuit.com--id-4422.pdf
cours-gratuit.com--id-4422.pdfcours-gratuit.com--id-4422.pdf
cours-gratuit.com--id-4422.pdf
lhoussainebouganfou
 
JRE , JDK and platform independent nature of JAVA
JRE , JDK and platform independent nature of JAVAJRE , JDK and platform independent nature of JAVA
JRE , JDK and platform independent nature of JAVA
Mehak Tawakley
 
Spring Boot
Spring BootSpring Boot
Spring Boot
HongSeong Jeon
 
Graal in GraalVM - A New JIT Compiler
Graal in GraalVM - A New JIT CompilerGraal in GraalVM - A New JIT Compiler
Graal in GraalVM - A New JIT Compiler
Koichi Sakata
 
Java Multithreading
Java MultithreadingJava Multithreading
Java Multithreading
Rajkattamuri
 
Asynchronous JavaScript Programming with Callbacks & Promises
Asynchronous JavaScript Programming with Callbacks & PromisesAsynchronous JavaScript Programming with Callbacks & Promises
Asynchronous JavaScript Programming with Callbacks & Promises
Hùng Nguyễn Huy
 
Node js introduction
Node js introductionNode js introduction
Node js introduction
Joseph de Castelnau
 
Spring Boot in Action
Spring Boot in Action Spring Boot in Action
Spring Boot in Action
Alex Movila
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Raghu nath
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
Rob O'Doherty
 
Spring boot
Spring bootSpring boot
Spring boot
Pradeep Shanmugam
 
Core Java
Core JavaCore Java
Core Java
Priyanka Pradhan
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
arvind pandey
 
Introduction à spring boot
Introduction à spring bootIntroduction à spring boot
Introduction à spring boot
Antoine Rey
 
NodeJS for Beginner
NodeJS for BeginnerNodeJS for Beginner
NodeJS for Beginner
Apaichon Punopas
 
Introduction Node.js
Introduction Node.jsIntroduction Node.js
Introduction Node.js
Erik van Appeldoorn
 
Mocking in Java with Mockito
Mocking in Java with MockitoMocking in Java with Mockito
Mocking in Java with Mockito
Richard Paul
 
Java 8 Lambda and Streams
Java 8 Lambda and StreamsJava 8 Lambda and Streams
Java 8 Lambda and Streams
Venkata Naga Ravi
 
Nodejs vatsal shah
Nodejs vatsal shahNodejs vatsal shah
Nodejs vatsal shah
Vatsal N Shah
 
Java Virtual Machine - Internal Architecture
Java Virtual Machine - Internal ArchitectureJava Virtual Machine - Internal Architecture
Java Virtual Machine - Internal Architecture
subnesh
 
JRE , JDK and platform independent nature of JAVA
JRE , JDK and platform independent nature of JAVAJRE , JDK and platform independent nature of JAVA
JRE , JDK and platform independent nature of JAVA
Mehak Tawakley
 
Graal in GraalVM - A New JIT Compiler
Graal in GraalVM - A New JIT CompilerGraal in GraalVM - A New JIT Compiler
Graal in GraalVM - A New JIT Compiler
Koichi Sakata
 
Java Multithreading
Java MultithreadingJava Multithreading
Java Multithreading
Rajkattamuri
 
Asynchronous JavaScript Programming with Callbacks & Promises
Asynchronous JavaScript Programming with Callbacks & PromisesAsynchronous JavaScript Programming with Callbacks & Promises
Asynchronous JavaScript Programming with Callbacks & Promises
Hùng Nguyễn Huy
 
Spring Boot in Action
Spring Boot in Action Spring Boot in Action
Spring Boot in Action
Alex Movila
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Raghu nath
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
Rob O'Doherty
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
arvind pandey
 
Introduction à spring boot
Introduction à spring bootIntroduction à spring boot
Introduction à spring boot
Antoine Rey
 
Mocking in Java with Mockito
Mocking in Java with MockitoMocking in Java with Mockito
Mocking in Java with Mockito
Richard Paul
 
Java Virtual Machine - Internal Architecture
Java Virtual Machine - Internal ArchitectureJava Virtual Machine - Internal Architecture
Java Virtual Machine - Internal Architecture
subnesh
 

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

Java unit 12
Java unit 12Java unit 12
Java unit 12
Shipra Swati
 
Threadnotes
ThreadnotesThreadnotes
Threadnotes
Himanshu Rajput
 
Multithreading programming in java
Multithreading programming in javaMultithreading programming in java
Multithreading programming in java
Elizabeth alexander
 
CSE 3146 M1- MULTI THREADING USING JAVA .pdf
CSE 3146 M1- MULTI THREADING USING JAVA  .pdfCSE 3146 M1- MULTI THREADING USING JAVA  .pdf
CSE 3146 M1- MULTI THREADING USING JAVA .pdf
universitypresidency
 
Java threads
Java threadsJava threads
Java threads
Prabhakaran V M
 
L22 multi-threading-introduction
L22 multi-threading-introductionL22 multi-threading-introduction
L22 multi-threading-introduction
teach4uin
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
junnubabu
 
Module 4 - Part 4 - Multithreaded Programming.pptx
Module 4 - Part 4 - Multithreaded Programming.pptxModule 4 - Part 4 - Multithreaded Programming.pptx
Module 4 - Part 4 - Multithreaded Programming.pptx
FahmaFamzin
 
Multithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of threadMultithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of thread
Kartik Dube
 
Multithreadingppt.pptx
Multithreadingppt.pptxMultithreadingppt.pptx
Multithreadingppt.pptx
HKShab
 
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx
sandhyakiran10
 
multithreading,thread and processinjava-210302183809.pptx
multithreading,thread and processinjava-210302183809.pptxmultithreading,thread and processinjava-210302183809.pptx
multithreading,thread and processinjava-210302183809.pptx
ArunPatrick2
 
Object-Oriented-Prog_MultiThreading.pptx
Object-Oriented-Prog_MultiThreading.pptxObject-Oriented-Prog_MultiThreading.pptx
Object-Oriented-Prog_MultiThreading.pptx
NasreenTaj20
 
Multithreading.pptx
Multithreading.pptxMultithreading.pptx
Multithreading.pptx
PragatiSutar4
 
Chap2 2 1
Chap2 2 1Chap2 2 1
Chap2 2 1
Hemo Chella
 
Thread Concept: Multithreading, Creating thread using thread
Thread Concept: Multithreading, Creating thread using threadThread Concept: Multithreading, Creating thread using thread
Thread Concept: Multithreading, Creating thread using thread
poongothai11
 
unit3multithreadingppt-copy-180122162204.pptx
unit3multithreadingppt-copy-180122162204.pptxunit3multithreadingppt-copy-180122162204.pptx
unit3multithreadingppt-copy-180122162204.pptx
ArunPatrick2
 
unit3 Exception Handling multithreadingppt.pptx
unit3 Exception Handling multithreadingppt.pptxunit3 Exception Handling multithreadingppt.pptx
unit3 Exception Handling multithreadingppt.pptx
ArunPatrick2
 
Session 7_MULTITHREADING in java example.ppt
Session 7_MULTITHREADING in java example.pptSession 7_MULTITHREADING in java example.ppt
Session 7_MULTITHREADING in java example.ppt
TabassumMaktum
 
Multithreading.pptx
Multithreading.pptxMultithreading.pptx
Multithreading.pptx
HemantSharma134028
 
Multithreading programming in java
Multithreading programming in javaMultithreading programming in java
Multithreading programming in java
Elizabeth alexander
 
CSE 3146 M1- MULTI THREADING USING JAVA .pdf
CSE 3146 M1- MULTI THREADING USING JAVA  .pdfCSE 3146 M1- MULTI THREADING USING JAVA  .pdf
CSE 3146 M1- MULTI THREADING USING JAVA .pdf
universitypresidency
 
L22 multi-threading-introduction
L22 multi-threading-introductionL22 multi-threading-introduction
L22 multi-threading-introduction
teach4uin
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
junnubabu
 
Module 4 - Part 4 - Multithreaded Programming.pptx
Module 4 - Part 4 - Multithreaded Programming.pptxModule 4 - Part 4 - Multithreaded Programming.pptx
Module 4 - Part 4 - Multithreaded Programming.pptx
FahmaFamzin
 
Multithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of threadMultithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of thread
Kartik Dube
 
Multithreadingppt.pptx
Multithreadingppt.pptxMultithreadingppt.pptx
Multithreadingppt.pptx
HKShab
 
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx
8.-OBJECT-ORIENTED-PROGRAMMING-USING-JAVA-Multithreading.pptx
sandhyakiran10
 
multithreading,thread and processinjava-210302183809.pptx
multithreading,thread and processinjava-210302183809.pptxmultithreading,thread and processinjava-210302183809.pptx
multithreading,thread and processinjava-210302183809.pptx
ArunPatrick2
 
Object-Oriented-Prog_MultiThreading.pptx
Object-Oriented-Prog_MultiThreading.pptxObject-Oriented-Prog_MultiThreading.pptx
Object-Oriented-Prog_MultiThreading.pptx
NasreenTaj20
 
Thread Concept: Multithreading, Creating thread using thread
Thread Concept: Multithreading, Creating thread using threadThread Concept: Multithreading, Creating thread using thread
Thread Concept: Multithreading, Creating thread using thread
poongothai11
 
unit3multithreadingppt-copy-180122162204.pptx
unit3multithreadingppt-copy-180122162204.pptxunit3multithreadingppt-copy-180122162204.pptx
unit3multithreadingppt-copy-180122162204.pptx
ArunPatrick2
 
unit3 Exception Handling multithreadingppt.pptx
unit3 Exception Handling multithreadingppt.pptxunit3 Exception Handling multithreadingppt.pptx
unit3 Exception Handling multithreadingppt.pptx
ArunPatrick2
 
Session 7_MULTITHREADING in java example.ppt
Session 7_MULTITHREADING in java example.pptSession 7_MULTITHREADING in java example.ppt
Session 7_MULTITHREADING in java example.ppt
TabassumMaktum
 
Ad

More from Edureka! (20)

What to learn during the 21 days Lockdown | Edureka
What to learn during the 21 days Lockdown | EdurekaWhat to learn during the 21 days Lockdown | Edureka
What to learn during the 21 days Lockdown | Edureka
Edureka!
 
Top 10 Dying Programming Languages in 2020 | Edureka
Top 10 Dying Programming Languages in 2020 | EdurekaTop 10 Dying Programming Languages in 2020 | Edureka
Top 10 Dying Programming Languages in 2020 | Edureka
Edureka!
 
Top 5 Trending Business Intelligence Tools | Edureka
Top 5 Trending Business Intelligence Tools | EdurekaTop 5 Trending Business Intelligence Tools | Edureka
Top 5 Trending Business Intelligence Tools | Edureka
Edureka!
 
Tableau Tutorial for Data Science | Edureka
Tableau Tutorial for Data Science | EdurekaTableau Tutorial for Data Science | Edureka
Tableau Tutorial for Data Science | Edureka
Edureka!
 
Python Programming Tutorial | Edureka
Python Programming Tutorial | EdurekaPython Programming Tutorial | Edureka
Python Programming Tutorial | Edureka
Edureka!
 
Top 5 PMP Certifications | Edureka
Top 5 PMP Certifications | EdurekaTop 5 PMP Certifications | Edureka
Top 5 PMP Certifications | Edureka
Edureka!
 
Top Maven Interview Questions in 2020 | Edureka
Top Maven Interview Questions in 2020 | EdurekaTop Maven Interview Questions in 2020 | Edureka
Top Maven Interview Questions in 2020 | Edureka
Edureka!
 
Linux Mint Tutorial | Edureka
Linux Mint Tutorial | EdurekaLinux Mint Tutorial | Edureka
Linux Mint Tutorial | Edureka
Edureka!
 
How to Deploy Java Web App in AWS| Edureka
How to Deploy Java Web App in AWS| EdurekaHow to Deploy Java Web App in AWS| Edureka
How to Deploy Java Web App in AWS| Edureka
Edureka!
 
Importance of Digital Marketing | Edureka
Importance of Digital Marketing | EdurekaImportance of Digital Marketing | Edureka
Importance of Digital Marketing | Edureka
Edureka!
 
RPA in 2020 | Edureka
RPA in 2020 | EdurekaRPA in 2020 | Edureka
RPA in 2020 | Edureka
Edureka!
 
Email Notifications in Jenkins | Edureka
Email Notifications in Jenkins | EdurekaEmail Notifications in Jenkins | Edureka
Email Notifications in Jenkins | Edureka
Edureka!
 
EA Algorithm in Machine Learning | Edureka
EA Algorithm in Machine Learning | EdurekaEA Algorithm in Machine Learning | Edureka
EA Algorithm in Machine Learning | Edureka
Edureka!
 
Cognitive AI Tutorial | Edureka
Cognitive AI Tutorial | EdurekaCognitive AI Tutorial | Edureka
Cognitive AI Tutorial | Edureka
Edureka!
 
AWS Cloud Practitioner Tutorial | Edureka
AWS Cloud Practitioner Tutorial | EdurekaAWS Cloud Practitioner Tutorial | Edureka
AWS Cloud Practitioner Tutorial | Edureka
Edureka!
 
Blue Prism Top Interview Questions | Edureka
Blue Prism Top Interview Questions | EdurekaBlue Prism Top Interview Questions | Edureka
Blue Prism Top Interview Questions | Edureka
Edureka!
 
Big Data on AWS Tutorial | Edureka
Big Data on AWS Tutorial | Edureka Big Data on AWS Tutorial | Edureka
Big Data on AWS Tutorial | Edureka
Edureka!
 
A star algorithm | A* Algorithm in Artificial Intelligence | Edureka
A star algorithm | A* Algorithm in Artificial Intelligence | EdurekaA star algorithm | A* Algorithm in Artificial Intelligence | Edureka
A star algorithm | A* Algorithm in Artificial Intelligence | Edureka
Edureka!
 
Kubernetes Installation on Ubuntu | Edureka
Kubernetes Installation on Ubuntu | EdurekaKubernetes Installation on Ubuntu | Edureka
Kubernetes Installation on Ubuntu | Edureka
Edureka!
 
Introduction to DevOps | Edureka
Introduction to DevOps | EdurekaIntroduction to DevOps | Edureka
Introduction to DevOps | Edureka
Edureka!
 
What to learn during the 21 days Lockdown | Edureka
What to learn during the 21 days Lockdown | EdurekaWhat to learn during the 21 days Lockdown | Edureka
What to learn during the 21 days Lockdown | Edureka
Edureka!
 
Top 10 Dying Programming Languages in 2020 | Edureka
Top 10 Dying Programming Languages in 2020 | EdurekaTop 10 Dying Programming Languages in 2020 | Edureka
Top 10 Dying Programming Languages in 2020 | Edureka
Edureka!
 
Top 5 Trending Business Intelligence Tools | Edureka
Top 5 Trending Business Intelligence Tools | EdurekaTop 5 Trending Business Intelligence Tools | Edureka
Top 5 Trending Business Intelligence Tools | Edureka
Edureka!
 
Tableau Tutorial for Data Science | Edureka
Tableau Tutorial for Data Science | EdurekaTableau Tutorial for Data Science | Edureka
Tableau Tutorial for Data Science | Edureka
Edureka!
 
Python Programming Tutorial | Edureka
Python Programming Tutorial | EdurekaPython Programming Tutorial | Edureka
Python Programming Tutorial | Edureka
Edureka!
 
Top 5 PMP Certifications | Edureka
Top 5 PMP Certifications | EdurekaTop 5 PMP Certifications | Edureka
Top 5 PMP Certifications | Edureka
Edureka!
 
Top Maven Interview Questions in 2020 | Edureka
Top Maven Interview Questions in 2020 | EdurekaTop Maven Interview Questions in 2020 | Edureka
Top Maven Interview Questions in 2020 | Edureka
Edureka!
 
Linux Mint Tutorial | Edureka
Linux Mint Tutorial | EdurekaLinux Mint Tutorial | Edureka
Linux Mint Tutorial | Edureka
Edureka!
 
How to Deploy Java Web App in AWS| Edureka
How to Deploy Java Web App in AWS| EdurekaHow to Deploy Java Web App in AWS| Edureka
How to Deploy Java Web App in AWS| Edureka
Edureka!
 
Importance of Digital Marketing | Edureka
Importance of Digital Marketing | EdurekaImportance of Digital Marketing | Edureka
Importance of Digital Marketing | Edureka
Edureka!
 
RPA in 2020 | Edureka
RPA in 2020 | EdurekaRPA in 2020 | Edureka
RPA in 2020 | Edureka
Edureka!
 
Email Notifications in Jenkins | Edureka
Email Notifications in Jenkins | EdurekaEmail Notifications in Jenkins | Edureka
Email Notifications in Jenkins | Edureka
Edureka!
 
EA Algorithm in Machine Learning | Edureka
EA Algorithm in Machine Learning | EdurekaEA Algorithm in Machine Learning | Edureka
EA Algorithm in Machine Learning | Edureka
Edureka!
 
Cognitive AI Tutorial | Edureka
Cognitive AI Tutorial | EdurekaCognitive AI Tutorial | Edureka
Cognitive AI Tutorial | Edureka
Edureka!
 
AWS Cloud Practitioner Tutorial | Edureka
AWS Cloud Practitioner Tutorial | EdurekaAWS Cloud Practitioner Tutorial | Edureka
AWS Cloud Practitioner Tutorial | Edureka
Edureka!
 
Blue Prism Top Interview Questions | Edureka
Blue Prism Top Interview Questions | EdurekaBlue Prism Top Interview Questions | Edureka
Blue Prism Top Interview Questions | Edureka
Edureka!
 
Big Data on AWS Tutorial | Edureka
Big Data on AWS Tutorial | Edureka Big Data on AWS Tutorial | Edureka
Big Data on AWS Tutorial | Edureka
Edureka!
 
A star algorithm | A* Algorithm in Artificial Intelligence | Edureka
A star algorithm | A* Algorithm in Artificial Intelligence | EdurekaA star algorithm | A* Algorithm in Artificial Intelligence | Edureka
A star algorithm | A* Algorithm in Artificial Intelligence | Edureka
Edureka!
 
Kubernetes Installation on Ubuntu | Edureka
Kubernetes Installation on Ubuntu | EdurekaKubernetes Installation on Ubuntu | Edureka
Kubernetes Installation on Ubuntu | Edureka
Edureka!
 
Introduction to DevOps | Edureka
Introduction to DevOps | EdurekaIntroduction to DevOps | Edureka
Introduction to DevOps | Edureka
Edureka!
 
Ad

Recently uploaded (20)

AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and ImplementationAI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
Christine Shepherd
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI ProfessionalOracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
Edge AI and Vision Alliance
 
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Impelsys Inc.
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training RoadblocksDown the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdfHow Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
Rejig Digital
 
Introduction to Typescript - GDG On Campus EUE
Introduction to Typescript - GDG On Campus EUEIntroduction to Typescript - GDG On Campus EUE
Introduction to Typescript - GDG On Campus EUE
Google Developer Group On Campus European Universities in Egypt
 
Oracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization ProgramOracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization Program
VICTOR MAESTRE RAMIREZ
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
Domino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use CasesDomino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use Cases
panagenda
 
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven InfrastructureNo-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FMEEnabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
Cisco ISE Performance, Scalability and Best Practices.pdf
Cisco ISE Performance, Scalability and Best Practices.pdfCisco ISE Performance, Scalability and Best Practices.pdf
Cisco ISE Performance, Scalability and Best Practices.pdf
superdpz
 
TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025
Suyash Joshi
 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 
Providing an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME FlowProviding an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME Flow
Safe Software
 
Trends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary MeekerTrends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary Meeker
Clive Dickens
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean accountYour startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and ImplementationAI Agents in Logistics and Supply Chain Applications Benefits and Implementation
AI Agents in Logistics and Supply Chain Applications Benefits and Implementation
Christine Shepherd
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI ProfessionalOracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
Edge AI and Vision Alliance
 
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Impelsys Inc.
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training RoadblocksDown the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdfHow Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
Rejig Digital
 
Oracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization ProgramOracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization Program
VICTOR MAESTRE RAMIREZ
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
Domino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use CasesDomino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use Cases
panagenda
 
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven InfrastructureNo-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
Enabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FMEEnabling BIM / GIS integrations with Other Systems with FME
Enabling BIM / GIS integrations with Other Systems with FME
Safe Software
 
Cisco ISE Performance, Scalability and Best Practices.pdf
Cisco ISE Performance, Scalability and Best Practices.pdfCisco ISE Performance, Scalability and Best Practices.pdf
Cisco ISE Performance, Scalability and Best Practices.pdf
superdpz
 
TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025
Suyash Joshi
 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 
Providing an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME FlowProviding an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME Flow
Safe Software
 
Trends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary MeekerTrends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary Meeker
Clive Dickens
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean accountYour startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 

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