SlideShare une entreprise Scribd logo
1  sur  12
Java

Multithreading in Java

By Nishant S Mevawala
What are Threads?
• A piece of code that run in concurrent with other
threads.
• Each thread is a statically ordered sequence of
instructions.
• Threads are being extensively used express
concurrency on both single and multiprocessors
machines.
• Programming a task having multiple threads of
control – Multithreading or Multithreaded
Programming.
2
Life Cycle of
Thread
Threads
• Java has built in thread support for
Multithreading
• Synchronization
• Thread Scheduling
• Inter-Thread Communication:
currentThread
yield
sleep

start
run
stop

setPriority
getPriority
suspend
resume

• Java Garbage Collector is a low-priority thread
Types of Priority
1. Thread.MIN_PRIORITY - 1
2. Thread.MAX_PRIORITY - 10
3. Thread.NORM_PRIORITY - 5
Born
Stop

Start
Run

Running

Yield

Sleep
Suspended
Wait

Runnable

Stop

Resume

Wait

Notify
Stop

Dead
Ways of Multithreading in Java
•
•

Create a class that extends the Thread class
Create a class that implements the Runnable interface

• 1st Method: Extending the Thread class
class MyThread extends Thread
{
public void run()
{
// thread body of execution

•
•

}
}
Creating thread:
MyThread thr1 = new MyThread();
Start Execution:
thr1.start();
Extends Method example
class A extends Thread
{
public void run()
{
for(int i=1;i<=5;i++)
{
System.out.println("t From Thread A: i= "+i);
}
System.out.println("Exit from A");
}
}
class B extends Thread
{
public void run()
{
for(int j=1;j<=5;j++)
{
System.out.println("t From Thread B: j= "+j);
}
System.out.println("Exit from B");
}
}
class C extends Thread
{
public void run()
{
for(int k=1;k<=5;k++)
{
System.out.println("t From Thread C: k= "+k);
}
System.out.println("Exit from C");
}
}

class ThreadTest
{
public static void main(String S[])
{
new A().start();
new B().start();
new C().start();
}
}
2nd method: Threads by implementing Runnable
interface
class ClassName implements Runnable
{
.....
public void run()
{
// thread body of execution
}
}
• Creating Object:
ClassName myObject = new ClassName();
• Creating Thread Object:
Thread thr1 = new Thread( myObject );
• Start Execution:
thr1.start();
Runnable method Example
class ththead implements Runnable
{
String nm;
Thread t;
public ththead(String st)
{
nm=st;
t=new Thread(this);
}
public void run()
{
for(int i=1;i<=5;i++)
{
System.out.println(nm + ":" +i);
}
}
}
class theadrun
{
public static void main(String args[])
{
ththead f1=new ththead("one");
ththead f2=new ththead("Two");
ththead f3=new ththead("Three");
f1.t.start();
f2.t.start();
f3.t.start();
}
}

Contenu connexe

Tendances

String and string buffer
String and string bufferString and string buffer
String and string buffer
kamal kotecha
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Raghu nath
 
Java Presentation
Java PresentationJava Presentation
Java Presentation
pm2214
 
String classes and its methods.20
String classes and its methods.20String classes and its methods.20
String classes and its methods.20
myrajendra
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
parag
 
Applet life cycle
Applet life cycleApplet life cycle
Applet life cycle
myrajendra
 

Tendances (20)

Java multi threading
Java multi threadingJava multi threading
Java multi threading
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
 
Java applet
Java appletJava applet
Java applet
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Java Presentation
Java PresentationJava Presentation
Java Presentation
 
String classes and its methods.20
String classes and its methods.20String classes and its methods.20
String classes and its methods.20
 
Introduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita GanesanIntroduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita Ganesan
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
Java threads
Java threadsJava threads
Java threads
 
Chap2 2 1
Chap2 2 1Chap2 2 1
Chap2 2 1
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Java Streams
Java StreamsJava Streams
Java Streams
 
Garbage collection
Garbage collectionGarbage collection
Garbage collection
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
 
String, string builder, string buffer
String, string builder, string bufferString, string builder, string buffer
String, string builder, string buffer
 
Applet life cycle
Applet life cycleApplet life cycle
Applet life cycle
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
 

En vedette

Files & IO in Java
Files & IO in JavaFiles & IO in Java
Files & IO in Java
CIB Egypt
 
IO In Java
IO In JavaIO In Java
IO In Java
parag
 

En vedette (20)

Threads concept in java
Threads concept in javaThreads concept in java
Threads concept in java
 
Java Course 10: Threads and Concurrency
Java Course 10: Threads and ConcurrencyJava Course 10: Threads and Concurrency
Java Course 10: Threads and Concurrency
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Java concurrency
Java concurrencyJava concurrency
Java concurrency
 
Java concurrency
Java concurrencyJava concurrency
Java concurrency
 
Multithreading in Java
Multithreading in JavaMultithreading in Java
Multithreading in Java
 
Correct and efficient synchronization of java thread
Correct and efficient synchronization of java threadCorrect and efficient synchronization of java thread
Correct and efficient synchronization of java thread
 
Linkedin Search_GK
Linkedin Search_GKLinkedin Search_GK
Linkedin Search_GK
 
Jvm Architecture
Jvm ArchitectureJvm Architecture
Jvm Architecture
 
Java Multithreading
Java MultithreadingJava Multithreading
Java Multithreading
 
Java Concurrency in Practice
Java Concurrency in PracticeJava Concurrency in Practice
Java Concurrency in Practice
 
Java virtual machine
Java virtual machineJava virtual machine
Java virtual machine
 
QSpiders - Memory (JVM architecture)
QSpiders - Memory (JVM architecture)QSpiders - Memory (JVM architecture)
QSpiders - Memory (JVM architecture)
 
Understanding JVM
Understanding JVMUnderstanding JVM
Understanding JVM
 
Files & IO in Java
Files & IO in JavaFiles & IO in Java
Files & IO in Java
 
Java virtual machine
Java virtual machineJava virtual machine
Java virtual machine
 
My History
My HistoryMy History
My History
 
Architecture diagram of jvm
Architecture diagram of jvmArchitecture diagram of jvm
Architecture diagram of jvm
 
IO In Java
IO In JavaIO In Java
IO In Java
 
Multithreading
Multithreading Multithreading
Multithreading
 

Similaire à Java Multi Thead Programming

Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
James Wong
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
Harry Potter
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
Young Alista
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
Tony Nguyen
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
Fraboni Ec
 

Similaire à Java Multi Thead Programming (20)

U4 JAVA.pptx
U4 JAVA.pptxU4 JAVA.pptx
U4 JAVA.pptx
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Thread
ThreadThread
Thread
 
Thread
ThreadThread
Thread
 
Mulitthread
MulitthreadMulitthread
Mulitthread
 
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
 
Lec 1.10 Object Oriented Programming
Lec 1.10 Object Oriented ProgrammingLec 1.10 Object Oriented Programming
Lec 1.10 Object Oriented Programming
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
econtent thread in java.pptx
econtent thread in java.pptxecontent thread in java.pptx
econtent thread in java.pptx
 
Multithreading programming in java
Multithreading programming in javaMultithreading programming in java
Multithreading programming in java
 
Java Threads: Lightweight Processes
Java Threads: Lightweight ProcessesJava Threads: Lightweight Processes
Java Threads: Lightweight Processes
 
Unit-3 MULTITHREADING-2.pdf
Unit-3 MULTITHREADING-2.pdfUnit-3 MULTITHREADING-2.pdf
Unit-3 MULTITHREADING-2.pdf
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
 
Concurrency with java
Concurrency with javaConcurrency with java
Concurrency with java
 
Threads in Java
Threads in JavaThreads in Java
Threads in Java
 

Plus de Nishant Mevawala (6)

Adv programming languages
Adv programming languagesAdv programming languages
Adv programming languages
 
Disaster Management - Technical
Disaster Management - TechnicalDisaster Management - Technical
Disaster Management - Technical
 
Drying & Cleaning Plant for reprocessing of plastics
Drying & Cleaning Plantfor reprocessing of plasticsDrying & Cleaning Plantfor reprocessing of plastics
Drying & Cleaning Plant for reprocessing of plastics
 
Visitor design patterns
Visitor design patternsVisitor design patterns
Visitor design patterns
 
WebSphere Application Server
WebSphere Application ServerWebSphere Application Server
WebSphere Application Server
 
Beginning of black berry technology
Beginning of black berry technologyBeginning of black berry technology
Beginning of black berry technology
 

Dernier

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
 

Dernier (20)

Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
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
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
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
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
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
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
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
 
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...
 
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
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
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, ...
 
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
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
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
 
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...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
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 Multi Thead Programming

  • 1. Java Multithreading in Java By Nishant S Mevawala
  • 2. What are Threads? • A piece of code that run in concurrent with other threads. • Each thread is a statically ordered sequence of instructions. • Threads are being extensively used express concurrency on both single and multiprocessors machines. • Programming a task having multiple threads of control – Multithreading or Multithreaded Programming. 2
  • 4. Threads • Java has built in thread support for Multithreading • Synchronization • Thread Scheduling • Inter-Thread Communication: currentThread yield sleep start run stop setPriority getPriority suspend resume • Java Garbage Collector is a low-priority thread
  • 5. Types of Priority 1. Thread.MIN_PRIORITY - 1 2. Thread.MAX_PRIORITY - 10 3. Thread.NORM_PRIORITY - 5
  • 7. Ways of Multithreading in Java • • Create a class that extends the Thread class Create a class that implements the Runnable interface • 1st Method: Extending the Thread class class MyThread extends Thread { public void run() { // thread body of execution • • } } Creating thread: MyThread thr1 = new MyThread(); Start Execution: thr1.start();
  • 8. Extends Method example class A extends Thread { public void run() { for(int i=1;i<=5;i++) { System.out.println("t From Thread A: i= "+i); } System.out.println("Exit from A"); } } class B extends Thread { public void run() { for(int j=1;j<=5;j++) { System.out.println("t From Thread B: j= "+j); } System.out.println("Exit from B"); } }
  • 9. class C extends Thread { public void run() { for(int k=1;k<=5;k++) { System.out.println("t From Thread C: k= "+k); } System.out.println("Exit from C"); } } class ThreadTest { public static void main(String S[]) { new A().start(); new B().start(); new C().start(); } }
  • 10. 2nd method: Threads by implementing Runnable interface class ClassName implements Runnable { ..... public void run() { // thread body of execution } } • Creating Object: ClassName myObject = new ClassName(); • Creating Thread Object: Thread thr1 = new Thread( myObject ); • Start Execution: thr1.start();
  • 11. Runnable method Example class ththead implements Runnable { String nm; Thread t; public ththead(String st) { nm=st; t=new Thread(this); } public void run() { for(int i=1;i<=5;i++) { System.out.println(nm + ":" +i); } } }
  • 12. class theadrun { public static void main(String args[]) { ththead f1=new ththead("one"); ththead f2=new ththead("Two"); ththead f3=new ththead("Three"); f1.t.start(); f2.t.start(); f3.t.start(); } }