SlideShare ist ein Scribd-Unternehmen logo
1 von 22
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

Weitere ähnliche Inhalte

Was ist angesagt?

Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
parag
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
kamal kotecha
 

Was ist angesagt? (20)

Inner classes in java
Inner classes in javaInner classes in java
Inner classes in java
 
Java Tutorial For Beginners - Step By Step | Java Basics | Java Certification...
Java Tutorial For Beginners - Step By Step | Java Basics | Java Certification...Java Tutorial For Beginners - Step By Step | Java Basics | Java Certification...
Java Tutorial For Beginners - Step By Step | Java Basics | Java Certification...
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
 
Java multi threading
Java multi threadingJava multi threading
Java multi threading
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Java Training | Java Tutorial for Beginners | Java Programming | Java Certifi...
Java Training | Java Tutorial for Beginners | Java Programming | Java Certifi...Java Training | Java Tutorial for Beginners | Java Programming | Java Certifi...
Java Training | Java Tutorial for Beginners | Java Programming | Java Certifi...
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
 
Java Threads
Java ThreadsJava Threads
Java Threads
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
 
Introduction to java
Introduction to java Introduction to java
Introduction to java
 
Java platform
Java platformJava platform
Java platform
 
L22 multi-threading-introduction
L22 multi-threading-introductionL22 multi-threading-introduction
L22 multi-threading-introduction
 
Java
JavaJava
Java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
JDK,JRE,JVM
JDK,JRE,JVMJDK,JRE,JVM
JDK,JRE,JVM
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Java threads
Java threadsJava threads
Java threads
 

Ähnlich wie Java Threads Tutorial | Multithreading In Java Tutorial | Java Tutorial For Beginners | Edureka

201 core java interview questions oo ps interview questions - javatpoint
201 core java interview questions   oo ps interview questions - javatpoint201 core java interview questions   oo ps interview questions - javatpoint
201 core java interview questions oo ps interview questions - javatpoint
ravi tyagi
 
Java-1st.pptx about Java technology before oops
Java-1st.pptx about Java technology before oopsJava-1st.pptx about Java technology before oops
Java-1st.pptx about Java technology before oops
buvanabala
 

Ähnlich wie Java Threads Tutorial | Multithreading In Java Tutorial | Java Tutorial For Beginners | Edureka (20)

Java Class 6 | Java Class 6 |Threads in Java| Applets | Swing GUI | JDBC | Ac...
Java Class 6 | Java Class 6 |Threads in Java| Applets | Swing GUI | JDBC | Ac...Java Class 6 | Java Class 6 |Threads in Java| Applets | Swing GUI | JDBC | Ac...
Java Class 6 | Java Class 6 |Threads in Java| Applets | Swing GUI | JDBC | Ac...
 
Java unit 12
Java unit 12Java unit 12
Java unit 12
 
201 core java interview questions oo ps interview questions - javatpoint
201 core java interview questions   oo ps interview questions - javatpoint201 core java interview questions   oo ps interview questions - javatpoint
201 core java interview questions oo ps interview questions - javatpoint
 
Java introduction
Java introductionJava introduction
Java introduction
 
Lec 3 01_aug13
Lec 3 01_aug13Lec 3 01_aug13
Lec 3 01_aug13
 
How to run java program without IDE
How to run java program without IDEHow to run java program without IDE
How to run java program without IDE
 
159747608 a-training-report-on
159747608 a-training-report-on159747608 a-training-report-on
159747608 a-training-report-on
 
Spring boot competitive tests
Spring boot competitive testsSpring boot competitive tests
Spring boot competitive tests
 
Spring boot competitive tests
Spring boot competitive testsSpring boot competitive tests
Spring boot competitive tests
 
JVM, JRE and Javac are the main part for the java program
 JVM, JRE and Javac are the main part for the java program JVM, JRE and Javac are the main part for the java program
JVM, JRE and Javac are the main part for the java program
 
Unit of competency
Unit of competencyUnit of competency
Unit of competency
 
java_threads.ppt
java_threads.pptjava_threads.ppt
java_threads.ppt
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
B.Sc. III(VI Sem) Advance Java Unit2: Appet
B.Sc. III(VI Sem) Advance Java Unit2: AppetB.Sc. III(VI Sem) Advance Java Unit2: Appet
B.Sc. III(VI Sem) Advance Java Unit2: Appet
 
Java-1st.pptx about Java technology before oops
Java-1st.pptx about Java technology before oopsJava-1st.pptx about Java technology before oops
Java-1st.pptx about Java technology before oops
 
1.introduction to java
1.introduction to java1.introduction to java
1.introduction to java
 
Basic java part_ii
Basic java part_iiBasic java part_ii
Basic java part_ii
 
Java 9 and the impact on Maven Projects (JavaOne 2016)
Java 9 and the impact on Maven Projects (JavaOne 2016)Java 9 and the impact on Maven Projects (JavaOne 2016)
Java 9 and the impact on Maven Projects (JavaOne 2016)
 
JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)JAVA Object Oriented Programming (OOP)
JAVA Object Oriented Programming (OOP)
 
Jsf tutorial
Jsf tutorialJsf tutorial
Jsf tutorial
 

Mehr von Edureka!

Mehr von 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
 
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
 
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
 
Tableau Tutorial for Data Science | Edureka
Tableau Tutorial for Data Science | EdurekaTableau Tutorial for Data Science | Edureka
Tableau Tutorial for Data Science | Edureka
 
Python Programming Tutorial | Edureka
Python Programming Tutorial | EdurekaPython Programming Tutorial | Edureka
Python Programming Tutorial | Edureka
 
Top 5 PMP Certifications | Edureka
Top 5 PMP Certifications | EdurekaTop 5 PMP Certifications | Edureka
Top 5 PMP Certifications | 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
 
Linux Mint Tutorial | Edureka
Linux Mint Tutorial | EdurekaLinux Mint Tutorial | Edureka
Linux Mint Tutorial | 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
 
Importance of Digital Marketing | Edureka
Importance of Digital Marketing | EdurekaImportance of Digital Marketing | Edureka
Importance of Digital Marketing | Edureka
 
RPA in 2020 | Edureka
RPA in 2020 | EdurekaRPA in 2020 | Edureka
RPA in 2020 | Edureka
 
Email Notifications in Jenkins | Edureka
Email Notifications in Jenkins | EdurekaEmail Notifications in Jenkins | Edureka
Email Notifications in Jenkins | Edureka
 
EA Algorithm in Machine Learning | Edureka
EA Algorithm in Machine Learning | EdurekaEA Algorithm in Machine Learning | Edureka
EA Algorithm in Machine Learning | Edureka
 
Cognitive AI Tutorial | Edureka
Cognitive AI Tutorial | EdurekaCognitive AI Tutorial | Edureka
Cognitive AI Tutorial | Edureka
 
AWS Cloud Practitioner Tutorial | Edureka
AWS Cloud Practitioner Tutorial | EdurekaAWS Cloud Practitioner Tutorial | Edureka
AWS Cloud Practitioner Tutorial | Edureka
 
Blue Prism Top Interview Questions | Edureka
Blue Prism Top Interview Questions | EdurekaBlue Prism Top Interview Questions | Edureka
Blue Prism Top Interview Questions | 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
 
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
 
Kubernetes Installation on Ubuntu | Edureka
Kubernetes Installation on Ubuntu | EdurekaKubernetes Installation on Ubuntu | Edureka
Kubernetes Installation on Ubuntu | Edureka
 
Introduction to DevOps | Edureka
Introduction to DevOps | EdurekaIntroduction to DevOps | Edureka
Introduction to DevOps | Edureka
 

Kürzlich hochgeladen

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 

Kürzlich hochgeladen (20)

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 

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