SlideShare una empresa de Scribd logo
1 de 14
Thread DhrubojyotiKayal
Independent, concurrent paths of execution through a program lightweight processes Allow multiple activities to coexist within a single process Java is the first mainstream programming language to explicitly include threading within the language itself, rather than treating threading as a facility of the underlying operating system, paving way for rapid growth of Java as a language of choice for writing servers. Primer
Every Java program has at least one thread -- the main thread. When a Java program starts, the JVM creates the main thread and calls the program's main() method within that thread The JVM also creates other threads that are mostly invisible to programmers  Threads associated with garbage collection, object finalization, and other JVM housekeeping tasks Threads everywhere
More responsive UI A Swing event handler which does a long running process, it will hang and blur the entire UI and will not manage to handle any other events. Trigger alternate threads and hand over the long running task Take advantage of multi-processor systems Systems with multiple processors are common these days even in desktops supported by all OSes. Threads help schedulers make optimal usage of these processors by scheduling tasks to idle processor   Why use threads?
Simplicity in design What will happen to web applications if they handle one request at a time?  Background processing How will you poll a FTP directory, look for an incoming file and trigger off some processing Why use threads?
Extend Thread class public class MyThread extends Thread { @Override public void run() { System.out.println("Hey I am running in a thread"); } public static void main(String a[]) { MyThread t = new MyThread(); t.start(); } } Your first thread
public class MyRunnable implements Runnable { @Override public void run() { System.out.println("Hey I am runnable thread"); } public static void main(String a[]) { MyRunnable t = new MyRunnable(); Thread t2 = new Thread(t); t2.start(); } } Implementing Runnable
public class MyThread extends Thread { private int max; private int min; public MyThread(int min , int max) { this.max = max; this.min = min; } @Override public void run() { int counter = min; while(counter <= max) { System.out.println(Thread.currentThread().getName() + " : " + counter ); counter++; } } public static void main(String a[]) { MyThread t1 = new MyThread(1,5); MyThread t2 = new MyThread(6,10); MyThread t3 = new MyThread(11,15); t1.start(); t2.start(); t3.start(); } } Thread in Action
public class SharedData{  	private String data = "HAPPY";  	public void accessData(){  		for(inti =0; i<20; i++) 		{  			data = data + " " + i + " ";  		}  System.out.println(data);  	}  }  Shared data
public class MyThread extends Thread{  	private SharedData data;  	public MyThread(SharedData data){  this.data = data;  	}  	public void run(){  data.accessData();  	}  }  Shared data
public class ThreadSynchronizationTest {  	public static void main(String[] args) { 	SharedData data = new SharedData(); 	MyThread one = new MyThread(data); 	one.start(); MyThread two = new 	MyThread(data); two.start();  	}  }  And the output is?? Shared data test
public synchronized void accessData(){  		for(inti =0; i<20; i++) 		{  			data = data + " " + i + " ";  		}  System.out.println(data);  	}  Synchronize
synchronized (this) for(inti =0; i<20; i++) 		{  			data = data + " " + i + " ";  		}  System.out.println(data);  } Synchronize block
Q&A

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Multithreading Presentation
Multithreading PresentationMultithreading Presentation
Multithreading Presentation
 
Threading
ThreadingThreading
Threading
 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread Synchronization
 
Threads c sharp
Threads c sharpThreads c sharp
Threads c sharp
 
Multithread Programing in Java
Multithread Programing in JavaMultithread Programing in Java
Multithread Programing in Java
 
Multi threading
Multi threadingMulti threading
Multi threading
 
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
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Multithread
MultithreadMultithread
Multithread
 
Multi-Threading
Multi-ThreadingMulti-Threading
Multi-Threading
 
Multi threading
Multi threadingMulti threading
Multi threading
 
Introduction+To+Java+Concurrency
Introduction+To+Java+ConcurrencyIntroduction+To+Java+Concurrency
Introduction+To+Java+Concurrency
 
Programming with Threads in Java
Programming with Threads in JavaProgramming with Threads in Java
Programming with Threads in Java
 
Thread priorities
Thread prioritiesThread priorities
Thread priorities
 
Python multithreading
Python multithreadingPython multithreading
Python multithreading
 
Introduction to TPL
Introduction to TPLIntroduction to TPL
Introduction to TPL
 
Threads in Java
Threads in JavaThreads in Java
Threads in Java
 
javathreads
javathreadsjavathreads
javathreads
 
Intro To .Net Threads
Intro To .Net ThreadsIntro To .Net Threads
Intro To .Net Threads
 
Threads concept in java
Threads concept in javaThreads concept in java
Threads concept in java
 

Destacado

Natural disasters final
Natural disasters finalNatural disasters final
Natural disasters finalVesela Ruseva
 
B ulgarian potatoes_2003 - final
B ulgarian potatoes_2003 - finalB ulgarian potatoes_2003 - final
B ulgarian potatoes_2003 - finalVesela Ruseva
 
Measures taken by local autorities
Measures taken by local autoritiesMeasures taken by local autorities
Measures taken by local autoritiesVesela Ruseva
 
Water sports in bulgaria
Water sports in bulgariaWater sports in bulgaria
Water sports in bulgariaVesela Ruseva
 
Life by the water presentation first meeting
Life by the water presentation  first meetingLife by the water presentation  first meeting
Life by the water presentation first meetingVesela Ruseva
 

Destacado (8)

Natural disasters final
Natural disasters finalNatural disasters final
Natural disasters final
 
B ulgarian potatoes_2003 - final
B ulgarian potatoes_2003 - finalB ulgarian potatoes_2003 - final
B ulgarian potatoes_2003 - final
 
Measures taken by local autorities
Measures taken by local autoritiesMeasures taken by local autorities
Measures taken by local autorities
 
Water plants rosi
Water plants rosiWater plants rosi
Water plants rosi
 
Pollution of burgas
Pollution of burgasPollution of burgas
Pollution of burgas
 
Water sports in bulgaria
Water sports in bulgariaWater sports in bulgaria
Water sports in bulgaria
 
Life by the water presentation first meeting
Life by the water presentation  first meetingLife by the water presentation  first meeting
Life by the water presentation first meeting
 
Heat Related Illnesses
Heat Related IllnessesHeat Related Illnesses
Heat Related Illnesses
 

Similar a 18 concurrency

Java programming PPT. .pptx
Java programming PPT.                 .pptxJava programming PPT.                 .pptx
Java programming PPT. .pptxcreativegamerz00
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaRaghu nath
 
Multithreading
MultithreadingMultithreading
Multithreadingbackdoor
 
Multi-threaded Programming in JAVA
Multi-threaded Programming in JAVAMulti-threaded Programming in JAVA
Multi-threaded Programming in JAVAVikram Kalyani
 
9.multi-threading latest(MB).ppt .
9.multi-threading latest(MB).ppt            .9.multi-threading latest(MB).ppt            .
9.multi-threading latest(MB).ppt .happycocoman
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaArafat Hossan
 
Java Performance, Threading and Concurrent Data Structures
Java Performance, Threading and Concurrent Data StructuresJava Performance, Threading and Concurrent Data Structures
Java Performance, Threading and Concurrent Data StructuresHitendra Kumar
 
Runnable interface.34
Runnable interface.34Runnable interface.34
Runnable interface.34myrajendra
 
Multithreading.pptx
Multithreading.pptxMultithreading.pptx
Multithreading.pptxssuserfcae42
 
Class notes(week 9) on multithreading
Class notes(week 9) on multithreadingClass notes(week 9) on multithreading
Class notes(week 9) on multithreadingKuntal Bhowmick
 
Class notes(week 9) on multithreading
Class notes(week 9) on multithreadingClass notes(week 9) on multithreading
Class notes(week 9) on multithreadingKuntal Bhowmick
 
Parallel Programming With Dot Net
Parallel Programming With Dot NetParallel Programming With Dot Net
Parallel Programming With Dot NetNeeraj Kaushik
 
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
 

Similar a 18 concurrency (20)

Java programming PPT. .pptx
Java programming PPT.                 .pptxJava programming PPT.                 .pptx
Java programming PPT. .pptx
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Md09 multithreading
Md09 multithreadingMd09 multithreading
Md09 multithreading
 
Multithreading
MultithreadingMultithreading
Multithreading
 
Multi-threaded Programming in JAVA
Multi-threaded Programming in JAVAMulti-threaded Programming in JAVA
Multi-threaded Programming in JAVA
 
advanced java ppt
advanced java pptadvanced java ppt
advanced java ppt
 
Threadnotes
ThreadnotesThreadnotes
Threadnotes
 
Java Multithreading
Java MultithreadingJava Multithreading
Java Multithreading
 
Intake 38 12
Intake 38 12Intake 38 12
Intake 38 12
 
Multithreading
MultithreadingMultithreading
Multithreading
 
9.multi-threading latest(MB).ppt .
9.multi-threading latest(MB).ppt            .9.multi-threading latest(MB).ppt            .
9.multi-threading latest(MB).ppt .
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Java unit 12
Java unit 12Java unit 12
Java unit 12
 
Java Performance, Threading and Concurrent Data Structures
Java Performance, Threading and Concurrent Data StructuresJava Performance, Threading and Concurrent Data Structures
Java Performance, Threading and Concurrent Data Structures
 
Runnable interface.34
Runnable interface.34Runnable interface.34
Runnable interface.34
 
Multithreading.pptx
Multithreading.pptxMultithreading.pptx
Multithreading.pptx
 
Class notes(week 9) on multithreading
Class notes(week 9) on multithreadingClass notes(week 9) on multithreading
Class notes(week 9) on multithreading
 
Class notes(week 9) on multithreading
Class notes(week 9) on multithreadingClass notes(week 9) on multithreading
Class notes(week 9) on multithreading
 
Parallel Programming With Dot Net
Parallel Programming With Dot NetParallel Programming With Dot Net
Parallel Programming With Dot Net
 
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
 

Más de dhrubo kayal

01 session tracking
01   session tracking01   session tracking
01 session trackingdhrubo kayal
 
03 handling requests
03 handling requests03 handling requests
03 handling requestsdhrubo kayal
 
02 up close with servlets
02 up close with servlets02 up close with servlets
02 up close with servletsdhrubo kayal
 
01 overview-servlets-and-environment-setup
01 overview-servlets-and-environment-setup01 overview-servlets-and-environment-setup
01 overview-servlets-and-environment-setupdhrubo kayal
 
14 initialization & cleanup
14   initialization & cleanup14   initialization & cleanup
14 initialization & cleanupdhrubo kayal
 
08 class and object
08   class and object08   class and object
08 class and objectdhrubo kayal
 
04 data types & variables
04   data types & variables04   data types & variables
04 data types & variablesdhrubo kayal
 
03 hello world with java
03   hello world with java03   hello world with java
03 hello world with javadhrubo kayal
 

Más de dhrubo kayal (20)

Cipla 20-09-2010
Cipla   20-09-2010Cipla   20-09-2010
Cipla 20-09-2010
 
01 session tracking
01   session tracking01   session tracking
01 session tracking
 
03 handling requests
03 handling requests03 handling requests
03 handling requests
 
02 up close with servlets
02 up close with servlets02 up close with servlets
02 up close with servlets
 
01 overview-servlets-and-environment-setup
01 overview-servlets-and-environment-setup01 overview-servlets-and-environment-setup
01 overview-servlets-and-environment-setup
 
19 reflection
19   reflection19   reflection
19 reflection
 
17 exceptions
17   exceptions17   exceptions
17 exceptions
 
16 containers
16   containers16   containers
16 containers
 
15 interfaces
15   interfaces15   interfaces
15 interfaces
 
14 initialization & cleanup
14   initialization & cleanup14   initialization & cleanup
14 initialization & cleanup
 
13 inheritance
13   inheritance13   inheritance
13 inheritance
 
12 encapsulation
12   encapsulation12   encapsulation
12 encapsulation
 
11 static
11   static11   static
11 static
 
10 access control
10   access control10   access control
10 access control
 
09 packages
09   packages09   packages
09 packages
 
08 class and object
08   class and object08   class and object
08 class and object
 
07 flow control
07   flow control07   flow control
07 flow control
 
05 operators
05   operators05   operators
05 operators
 
04 data types & variables
04   data types & variables04   data types & variables
04 data types & variables
 
03 hello world with java
03   hello world with java03   hello world with java
03 hello world with java
 

18 concurrency

  • 2. Independent, concurrent paths of execution through a program lightweight processes Allow multiple activities to coexist within a single process Java is the first mainstream programming language to explicitly include threading within the language itself, rather than treating threading as a facility of the underlying operating system, paving way for rapid growth of Java as a language of choice for writing servers. Primer
  • 3. Every Java program has at least one thread -- the main thread. When a Java program starts, the JVM creates the main thread and calls the program's main() method within that thread The JVM also creates other threads that are mostly invisible to programmers Threads associated with garbage collection, object finalization, and other JVM housekeeping tasks Threads everywhere
  • 4. More responsive UI A Swing event handler which does a long running process, it will hang and blur the entire UI and will not manage to handle any other events. Trigger alternate threads and hand over the long running task Take advantage of multi-processor systems Systems with multiple processors are common these days even in desktops supported by all OSes. Threads help schedulers make optimal usage of these processors by scheduling tasks to idle processor Why use threads?
  • 5. Simplicity in design What will happen to web applications if they handle one request at a time? Background processing How will you poll a FTP directory, look for an incoming file and trigger off some processing Why use threads?
  • 6. Extend Thread class public class MyThread extends Thread { @Override public void run() { System.out.println("Hey I am running in a thread"); } public static void main(String a[]) { MyThread t = new MyThread(); t.start(); } } Your first thread
  • 7. public class MyRunnable implements Runnable { @Override public void run() { System.out.println("Hey I am runnable thread"); } public static void main(String a[]) { MyRunnable t = new MyRunnable(); Thread t2 = new Thread(t); t2.start(); } } Implementing Runnable
  • 8. public class MyThread extends Thread { private int max; private int min; public MyThread(int min , int max) { this.max = max; this.min = min; } @Override public void run() { int counter = min; while(counter <= max) { System.out.println(Thread.currentThread().getName() + " : " + counter ); counter++; } } public static void main(String a[]) { MyThread t1 = new MyThread(1,5); MyThread t2 = new MyThread(6,10); MyThread t3 = new MyThread(11,15); t1.start(); t2.start(); t3.start(); } } Thread in Action
  • 9. public class SharedData{ private String data = "HAPPY"; public void accessData(){ for(inti =0; i<20; i++) { data = data + " " + i + " "; } System.out.println(data); } } Shared data
  • 10. public class MyThread extends Thread{ private SharedData data; public MyThread(SharedData data){ this.data = data; } public void run(){ data.accessData(); } } Shared data
  • 11. public class ThreadSynchronizationTest { public static void main(String[] args) { SharedData data = new SharedData(); MyThread one = new MyThread(data); one.start(); MyThread two = new MyThread(data); two.start(); } } And the output is?? Shared data test
  • 12. public synchronized void accessData(){ for(inti =0; i<20; i++) { data = data + " " + i + " "; } System.out.println(data); } Synchronize
  • 13. synchronized (this) for(inti =0; i<20; i++) { data = data + " " + i + " "; } System.out.println(data); } Synchronize block
  • 14. Q&A