SlideShare a Scribd company logo
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()
Multi threading
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

PDF
[Curso Java Basico] Aula 69: Criando varias Threads + metodos isAlive e join
PPTX
Multithreaded programming
PDF
[Curso Java Basico] Aula 70: Threads: Definindo prioridades
PPT
Python multithreading session 9 - shanmugam
PDF
Python multithreaded programming
PPT
Threads in java
[Curso Java Basico] Aula 69: Criando varias Threads + metodos isAlive e join
Multithreaded programming
[Curso Java Basico] Aula 70: Threads: Definindo prioridades
Python multithreading session 9 - shanmugam
Python multithreaded programming
Threads in java

What's hot (20)

PPT
Thread priorities35
PPTX
Python Training in Bangalore | Multi threading | Learnbay.in
PPTX
Thread priorities
PDF
Multithreading Introduction and Lifecyle of thread
PPTX
Multi threading
PPT
Threads in Java
PPTX
L22 multi-threading-introduction
ODP
Multithreading Concepts
PPTX
Internet Programming with Java
PDF
Multithreading in Java
PPTX
MULTI THREADING IN JAVA
PPSX
Multithreading in-java
PPT
12 multi-threading
 
PPTX
Multithreading in java
PPT
Java Multithreading and Concurrency
PPT
Java Multithreading
PPT
Java thread
PPTX
.NET Multithreading/Multitasking
PPTX
Java осень 2012 лекция 2
Thread priorities35
Python Training in Bangalore | Multi threading | Learnbay.in
Thread priorities
Multithreading Introduction and Lifecyle of thread
Multi threading
Threads in Java
L22 multi-threading-introduction
Multithreading Concepts
Internet Programming with Java
Multithreading in Java
MULTI THREADING IN JAVA
Multithreading in-java
12 multi-threading
 
Multithreading in java
Java Multithreading and Concurrency
Java Multithreading
Java thread
.NET Multithreading/Multitasking
Java осень 2012 лекция 2
Ad

Similar to Multi threading (20)

PPT
multhi threading concept in oops through java
PPTX
OOPS object oriented programming UNIT-4.pptx
PPTX
Java Multithreading - how to create threads
PPTX
java.pptxytbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
PPTX
multithreading to be used in java with good programs.pptx
PPT
java multi threading and synchronisation.ppt
PPT
Threads in java, Multitasking and Multithreading
PPTX
Multithreading
PPTX
Java Multithreading.pptx
PPTX
advanced java ppt
PPT
Session 7_MULTITHREADING in java example.ppt
PPT
Session 7_MULTITHREADING in java example.ppt
PPTX
Multithreading.pptx
PPTX
Java programming PPT. .pptx
PPT
Runnable interface.34
PPT
PPTX
Multithreading in Java Object Oriented Programming language
PPTX
Threading
PPT
Thread 1
PPTX
Multithreading in java
multhi threading concept in oops through java
OOPS object oriented programming UNIT-4.pptx
Java Multithreading - how to create threads
java.pptxytbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
multithreading to be used in java with good programs.pptx
java multi threading and synchronisation.ppt
Threads in java, Multitasking and Multithreading
Multithreading
Java Multithreading.pptx
advanced java ppt
Session 7_MULTITHREADING in java example.ppt
Session 7_MULTITHREADING in java example.ppt
Multithreading.pptx
Java programming PPT. .pptx
Runnable interface.34
Multithreading in Java Object Oriented Programming language
Threading
Thread 1
Multithreading in java
Ad

Recently uploaded (20)

PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPTX
Welding lecture in detail for understanding
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
Geodesy 1.pptx...............................................
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
web development for engineering and engineering
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPT
Mechanical Engineering MATERIALS Selection
PPTX
Sustainable Sites - Green Building Construction
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Welding lecture in detail for understanding
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Model Code of Practice - Construction Work - 21102022 .pdf
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
R24 SURVEYING LAB MANUAL for civil enggi
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Geodesy 1.pptx...............................................
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
web development for engineering and engineering
CYBER-CRIMES AND SECURITY A guide to understanding
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Mechanical Engineering MATERIALS Selection
Sustainable Sites - Green Building Construction

Multi threading

  • 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(); • } • }
  • 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(); } }