SlideShare a Scribd company logo
1 of 22
Problem solving using
Multi Threading
Topics in this slide
Multi Processing
Multi Threading
Life Cycle of Thread
How to create Thread
Some Methods assosiated with Thread
getName(),setName()
getPriority(),set Priority()
sleep(),join()
problem
Multi Tasking
Multi Processing Multi Threading
• Multi Processing= It is process based Multi
Tasking and it is OS level Multi Tasking.
• When two independent tasks are performed
simultaneously on system is called Multiprocessing
• Example=In a software industry employees are
used to work by listening to music.
• In this case work may be writing code, designing
reports, sending mail, etc but these Along with music
is running parallelly in background.
Multi Threading=Multi Threading is the program level Multi Tasking
It is the process of executing multiple threads simultaneously.
Multi Threading is used for execution of program in less time.
Defaultly Main Thread is responsible for execution of entire code unless the sub Threads are created
Public class Main{
Public static void main(String[] args){
Statement 1
Statement 2
Statement 3
Statement 4
}
}
In this sample code no
sub threads are created so, the
entire code is executed by Main
thread
Let the time taken by the execution
is 4 sec
Public class Main{
Public static void main(String[] args){
Statement 1-----------t1 executes statement1
Statement 2------------t2 executes statement 2
Statement 3------------t3 executes statement 3
Statement 4-------------t4 executes statement 4
t1,t2,t3,t4;
}
}
In this sample code 4 sub threads are
created(t1,t2,t3,t4) so, the entire code is
executed by Main thread as well as sub
threads
Let the time taken by the execution is less than
the previous case.
Life cycle of
Thread
• Threads can be created in two ways
• Extending Thread class
• Implementing Runnable Interface
• We cannot interpret the order of flow of thread
execution
Thread creation in Java
public class Main extends Thread{
//@ overrided method
public void run(){
System.out.println("Thread name is "+getName());
}
public static void main(String[] args){
Main t1=new Main(); //Thread t1 is created
Main t2=new Main(); // Thread t2 is created
t1.start();
t2.start();
}
}
getName() method returns the name of the Thread
start() method calls the run method
How to set name to the Thread ?
If you want to know which specific thread is executing then you need to set name for the thread. This helps you
to understand which thread is executing.
public class Main extends Thread{
//@ overrided method
public void run(){
System.out.println("Thread name is "+getName());
}
public static void main(String[] args){
Main t1=new Main(); //Thread t1 is created
Main t2=new Main(); // Thread t2 is created
t1.setName("CSE");
t2.setName("ECE");
t1.start();
t2.start();
}
}
Thread
Priority
How to know the priority of Thread ?
public class Main extends Thread{
//@ overrided method
public void run(){
System.out.println("Thread name is "+getName()+" priority is "+getPriority());
}
public static void main(String[] args){
Main t1=new Main(); //Thread t1 is created
Main t2=new Main(); // Thread t2 is created
t1.setName("CSE");
t2.setName("ECE");
t1.start();
t2.start();
}
}
getPriority() method returns the priority of thread
How to set priority to Thread ?
public class Main extends Thread{
//@ overrided method
public void run(){
System.out.println("Thread name is "+getName()+" priority is "+getPriority());
}
public static void main(String[] args){
Main t1=new Main(); //Thread t1 is created
Main t2=new Main(); // Thread t2 is created
t1.setPriority(Thread.MIN_PRIORITY);//Priority is 1
t2.setPriority(Thread.MAX_PRIORITY);//Priority is 10
t1.setName("CSE");
t2.setName("ECE");
t1.start();
t2.start();
}
}
setPriority(Thread.(MAX or MIN or NORM)_PRIORITY) method used to set priority to thread
• Sleep()
• The sleep() method of Thread class is used to sleep a thread for the specified amount of
time.
• public class Main extends Thread{
• //@ overrided method
• public void run(){
• try{
• for(int i=0;i<5;i=i+1){
• sleep(1000) //1000ms=1s
• System.out.println("Thread name is "+getName());
• }
• }
• catch(Exception e){
• }
• }
• public static void main(String[] args){
• Main t1=new Main(); //Thread t1 is created
• t1.start();
• }
• }
Join()
Without join() method
public class Main extends Thread
{
public void run(){
for(int i=0;i<5;i=i+1){
System.out.println(Thread.currentThread().getName()+" "+i);
}
}
public static void main(String[] args) {
Main t1=new Main();
Main t2=new Main();
t1.start();
t2.start();
}
}
With join() method
public class Main extends Thread
{
public void run(){
for(int i=0;i<5;i=i+1){
System.out.println(Thread.currentThread().getName()+" "+i);
}
}
public static void main(String[] args) {
Main t1=new Main();
Main t2=new Main();
t1.start();
try{
t1.join();
}catch(Exception e){
}
t2.start();
}
Problem
Write a program for addition of two numbers and product of two numbers using Threads
Expaination
Sum(a,b) performed by thread t1
Product(a,b) performed by thread t2
Main thread displays the sum and product value of two numbers
class Number{
int a;
int b;
int s;
int p;
Number(int a,int b){
this.a=a;
this.b=b;
}
public void sum(){
s=a+b;
}
public void multiply(){
p=a*b;
}
public void display(){
System.out.println("sum is "+s);
System.out.println("product is "+p);
}
}
class ThreadA extends Thread{
Number num;
ThreadA(Number num){
this.num=num;
}
}
public class Main{
public static void main(String[] args){
Number num=new Number(1,2);
ThreadA t1=new ThreadA(num){
public void run(){
num.sum();
}
};
ThreadA t2=new ThreadA(num){
public void run(){
num.multiply();
}
};
t1.start();
t2.start();
try{
t2.join();
}catch(Exception e){
}
num.display();
}
}

More Related Content

What's hot

Thread priorities35
Thread priorities35Thread priorities35
Thread priorities35myrajendra
 
Python Training in Bangalore | Multi threading | Learnbay.in
Python Training in Bangalore | Multi threading | Learnbay.inPython Training in Bangalore | Multi threading | Learnbay.in
Python Training in Bangalore | Multi threading | Learnbay.inLearnbayin
 
Multithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of threadMultithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of threadKartik Dube
 
L22 multi-threading-introduction
L22 multi-threading-introductionL22 multi-threading-introduction
L22 multi-threading-introductionteach4uin
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVAVINOTH R
 
Multithreading in-java
Multithreading in-javaMultithreading in-java
Multithreading in-javaaalipalh
 
12 multi-threading
12 multi-threading12 multi-threading
12 multi-threadingAPU
 
Java Multithreading and Concurrency
Java Multithreading and ConcurrencyJava Multithreading and Concurrency
Java Multithreading and ConcurrencyRajesh Ananda Kumar
 
Java Multithreading
Java MultithreadingJava Multithreading
Java MultithreadingRajkattamuri
 
.NET Multithreading/Multitasking
.NET Multithreading/Multitasking.NET Multithreading/Multitasking
.NET Multithreading/MultitaskingSasha Kravchuk
 
Java осень 2012 лекция 2
Java осень 2012 лекция 2Java осень 2012 лекция 2
Java осень 2012 лекция 2Technopark
 

What's hot (20)

Thread priorities35
Thread priorities35Thread priorities35
Thread priorities35
 
Python Training in Bangalore | Multi threading | Learnbay.in
Python Training in Bangalore | Multi threading | Learnbay.inPython Training in Bangalore | Multi threading | Learnbay.in
Python Training in Bangalore | Multi threading | Learnbay.in
 
Thread priorities
Thread prioritiesThread priorities
Thread priorities
 
Threads in python
Threads in pythonThreads in python
Threads in python
 
Multithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of threadMultithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of thread
 
Multi threading
Multi threadingMulti threading
Multi threading
 
Threads in Java
Threads in JavaThreads in Java
Threads in Java
 
L22 multi-threading-introduction
L22 multi-threading-introductionL22 multi-threading-introduction
L22 multi-threading-introduction
 
Multithreading Concepts
Multithreading ConceptsMultithreading Concepts
Multithreading Concepts
 
Internet Programming with Java
Internet Programming with JavaInternet Programming with Java
Internet Programming with 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
 
Multithreading in-java
Multithreading in-javaMultithreading in-java
Multithreading in-java
 
12 multi-threading
12 multi-threading12 multi-threading
12 multi-threading
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Java Multithreading and Concurrency
Java Multithreading and ConcurrencyJava Multithreading and Concurrency
Java Multithreading and Concurrency
 
Java Multithreading
Java MultithreadingJava Multithreading
Java Multithreading
 
Java thread
Java threadJava thread
Java thread
 
.NET Multithreading/Multitasking
.NET Multithreading/Multitasking.NET Multithreading/Multitasking
.NET Multithreading/Multitasking
 
Java осень 2012 лекция 2
Java осень 2012 лекция 2Java осень 2012 лекция 2
Java осень 2012 лекция 2
 

Similar to Problem solving using Multi Threading in Java

OOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptxOOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptxArulmozhivarman8
 
Threads in java, Multitasking and Multithreading
Threads in java, Multitasking and MultithreadingThreads in java, Multitasking and Multithreading
Threads in java, Multitasking and Multithreadingssusere538f7
 
Java Multithreading.pptx
Java Multithreading.pptxJava Multithreading.pptx
Java Multithreading.pptxRanjithaM32
 
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.pptTabassumMaktum
 
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.pptTabassumMaktum
 
Java programming PPT. .pptx
Java programming PPT.                 .pptxJava programming PPT.                 .pptx
Java programming PPT. .pptxcreativegamerz00
 
Runnable interface.34
Runnable interface.34Runnable interface.34
Runnable interface.34myrajendra
 
Multithreading in Java Object Oriented Programming language
Multithreading in Java Object Oriented Programming languageMultithreading in Java Object Oriented Programming language
Multithreading in Java Object Oriented Programming languagearnavytstudio2814
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaMonika Mishra
 
Java class 6
Java class 6Java class 6
Java class 6Edureka!
 
Java multi threading
Java multi threadingJava multi threading
Java multi threadingRaja Sekhar
 
Class notes(week 9) on multithreading
Class notes(week 9) on multithreadingClass notes(week 9) on multithreading
Class notes(week 9) on multithreadingKuntal Bhowmick
 

Similar to Problem solving using Multi Threading in Java (20)

OOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptxOOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptx
 
Threads in java, Multitasking and Multithreading
Threads in java, Multitasking and MultithreadingThreads in java, Multitasking and Multithreading
Threads in java, Multitasking and Multithreading
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Java Multithreading.pptx
Java Multithreading.pptxJava Multithreading.pptx
Java Multithreading.pptx
 
advanced java ppt
advanced java pptadvanced java ppt
advanced java ppt
 
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
 
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
 
Multithreading.pptx
Multithreading.pptxMultithreading.pptx
Multithreading.pptx
 
Java programming PPT. .pptx
Java programming PPT.                 .pptxJava programming PPT.                 .pptx
Java programming PPT. .pptx
 
Runnable interface.34
Runnable interface.34Runnable interface.34
Runnable interface.34
 
Threads
ThreadsThreads
Threads
 
Multithreading in Java Object Oriented Programming language
Multithreading in Java Object Oriented Programming languageMultithreading in Java Object Oriented Programming language
Multithreading in Java Object Oriented Programming language
 
Threading
ThreadingThreading
Threading
 
Thread 1
Thread 1Thread 1
Thread 1
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Java class 6
Java class 6Java class 6
Java class 6
 
Java multi threading
Java multi threadingJava multi threading
Java multi threading
 
Thread model in java
Thread model in javaThread model in java
Thread model in java
 
Threading concepts
Threading conceptsThreading concepts
Threading concepts
 
Class notes(week 9) on multithreading
Class notes(week 9) on multithreadingClass notes(week 9) on multithreading
Class notes(week 9) on multithreading
 

Recently uploaded

pipeline in computer architecture design
pipeline in computer architecture  designpipeline in computer architecture  design
pipeline in computer architecture designssuser87fa0c1
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfAsst.prof M.Gokilavani
 
Effects of rheological properties on mixing
Effects of rheological properties on mixingEffects of rheological properties on mixing
Effects of rheological properties on mixingviprabot1
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHC Sai Kiran
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...Chandu841456
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 

Recently uploaded (20)

pipeline in computer architecture design
pipeline in computer architecture  designpipeline in computer architecture  design
pipeline in computer architecture design
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
 
Effects of rheological properties on mixing
Effects of rheological properties on mixingEffects of rheological properties on mixing
Effects of rheological properties on mixing
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECH
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 

Problem solving using Multi Threading in Java

  • 2. Topics in this slide Multi Processing Multi Threading Life Cycle of Thread How to create Thread Some Methods assosiated with Thread getName(),setName() getPriority(),set Priority() sleep(),join() problem
  • 4. • Multi Processing= It is process based Multi Tasking and it is OS level Multi Tasking. • When two independent tasks are performed simultaneously on system is called Multiprocessing • Example=In a software industry employees are used to work by listening to music. • In this case work may be writing code, designing reports, sending mail, etc but these Along with music is running parallelly in background.
  • 5. Multi Threading=Multi Threading is the program level Multi Tasking It is the process of executing multiple threads simultaneously. Multi Threading is used for execution of program in less time. Defaultly Main Thread is responsible for execution of entire code unless the sub Threads are created
  • 6. Public class Main{ Public static void main(String[] args){ Statement 1 Statement 2 Statement 3 Statement 4 } } In this sample code no sub threads are created so, the entire code is executed by Main thread Let the time taken by the execution is 4 sec Public class Main{ Public static void main(String[] args){ Statement 1-----------t1 executes statement1 Statement 2------------t2 executes statement 2 Statement 3------------t3 executes statement 3 Statement 4-------------t4 executes statement 4 t1,t2,t3,t4; } } In this sample code 4 sub threads are created(t1,t2,t3,t4) so, the entire code is executed by Main thread as well as sub threads Let the time taken by the execution is less than the previous case.
  • 8. • Threads can be created in two ways • Extending Thread class • Implementing Runnable Interface • We cannot interpret the order of flow of thread execution
  • 9. Thread creation in Java public class Main extends Thread{ //@ overrided method public void run(){ System.out.println("Thread name is "+getName()); } public static void main(String[] args){ Main t1=new Main(); //Thread t1 is created Main t2=new Main(); // Thread t2 is created t1.start(); t2.start(); } } getName() method returns the name of the Thread start() method calls the run method
  • 10. How to set name to the Thread ? If you want to know which specific thread is executing then you need to set name for the thread. This helps you to understand which thread is executing. public class Main extends Thread{ //@ overrided method public void run(){ System.out.println("Thread name is "+getName()); } public static void main(String[] args){ Main t1=new Main(); //Thread t1 is created Main t2=new Main(); // Thread t2 is created t1.setName("CSE"); t2.setName("ECE"); t1.start(); t2.start(); } }
  • 12. How to know the priority of Thread ? public class Main extends Thread{ //@ overrided method public void run(){ System.out.println("Thread name is "+getName()+" priority is "+getPriority()); } public static void main(String[] args){ Main t1=new Main(); //Thread t1 is created Main t2=new Main(); // Thread t2 is created t1.setName("CSE"); t2.setName("ECE"); t1.start(); t2.start(); } } getPriority() method returns the priority of thread
  • 13. How to set priority to Thread ? public class Main extends Thread{ //@ overrided method public void run(){ System.out.println("Thread name is "+getName()+" priority is "+getPriority()); } public static void main(String[] args){ Main t1=new Main(); //Thread t1 is created Main t2=new Main(); // Thread t2 is created t1.setPriority(Thread.MIN_PRIORITY);//Priority is 1 t2.setPriority(Thread.MAX_PRIORITY);//Priority is 10 t1.setName("CSE"); t2.setName("ECE"); t1.start(); t2.start(); } } setPriority(Thread.(MAX or MIN or NORM)_PRIORITY) method used to set priority to thread
  • 14. • Sleep() • The sleep() method of Thread class is used to sleep a thread for the specified amount of time.
  • 15. • public class Main extends Thread{ • //@ overrided method • public void run(){ • try{ • for(int i=0;i<5;i=i+1){ • sleep(1000) //1000ms=1s • System.out.println("Thread name is "+getName()); • } • } • catch(Exception e){ • } • } • public static void main(String[] args){ • Main t1=new Main(); //Thread t1 is created • t1.start(); • } • }
  • 17.
  • 18. Without join() method public class Main extends Thread { public void run(){ for(int i=0;i<5;i=i+1){ System.out.println(Thread.currentThread().getName()+" "+i); } } public static void main(String[] args) { Main t1=new Main(); Main t2=new Main(); t1.start(); t2.start(); } }
  • 19. With join() method public class Main extends Thread { public void run(){ for(int i=0;i<5;i=i+1){ System.out.println(Thread.currentThread().getName()+" "+i); } } public static void main(String[] args) { Main t1=new Main(); Main t2=new Main(); t1.start(); try{ t1.join(); }catch(Exception e){ } t2.start(); }
  • 20. Problem Write a program for addition of two numbers and product of two numbers using Threads Expaination Sum(a,b) performed by thread t1 Product(a,b) performed by thread t2 Main thread displays the sum and product value of two numbers
  • 21. class Number{ int a; int b; int s; int p; Number(int a,int b){ this.a=a; this.b=b; } public void sum(){ s=a+b; } public void multiply(){ p=a*b; } public void display(){ System.out.println("sum is "+s); System.out.println("product is "+p); } }
  • 22. class ThreadA extends Thread{ Number num; ThreadA(Number num){ this.num=num; } } public class Main{ public static void main(String[] args){ Number num=new Number(1,2); ThreadA t1=new ThreadA(num){ public void run(){ num.sum(); } }; ThreadA t2=new ThreadA(num){ public void run(){ num.multiply(); } }; t1.start(); t2.start(); try{ t2.join(); }catch(Exception e){ } num.display(); } }