Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

18 concurrency


Published on

  • Be the first to comment

18 concurrency

  1. 1. Thread<br />DhrubojyotiKayal<br />
  2. 2. Independent, concurrent paths of execution through a program<br />lightweight processes<br />Allow multiple activities to coexist within a single process<br />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.<br />Primer<br />
  3. 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<br />The JVM also creates other threads that are mostly invisible to programmers <br />Threads associated with garbage collection, object finalization, and other JVM housekeeping tasks<br />Threads everywhere<br />
  4. 4. More responsive UI<br />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<br />Take advantage of multi-processor systems<br />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 <br />Why use threads?<br />
  5. 5. Simplicity in design<br />What will happen to web applications if they handle one request at a time? <br />Background processing<br />How will you poll a FTP directory, look for an incoming file and trigger off some processing<br />Why use threads?<br />
  6. 6. Extend Thread class<br />public class MyThread extends Thread {<br />@Override<br />public void run() {<br />System.out.println("Hey I am running in a thread");<br />}<br />public static void main(String a[]) {<br />MyThread t = new MyThread();<br />t.start();<br />}<br />}<br />Your first thread<br />
  7. 7. public class MyRunnable implements Runnable {<br />@Override<br />public void run() {<br />System.out.println("Hey I am runnable thread");<br />}<br />public static void main(String a[]) {<br />MyRunnable t = new MyRunnable();<br />Thread t2 = new Thread(t);<br />t2.start();<br />}<br />}<br />Implementing Runnable<br />
  8. 8. public class MyThread extends Thread {<br />private int max;<br />private int min;<br />public MyThread(int min , int max) {<br />this.max = max;<br />this.min = min;<br />}<br />@Override<br />public void run() {<br />int counter = min;<br />while(counter <= max) {<br />System.out.println(Thread.currentThread().getName() + " : " + counter );<br />counter++;<br />}<br />}<br />public static void main(String a[]) {<br />MyThread t1 = new MyThread(1,5);<br />MyThread t2 = new MyThread(6,10);<br />MyThread t3 = new MyThread(11,15);<br />t1.start();<br />t2.start();<br />t3.start();<br />}<br />}<br />Thread in Action<br />
  9. 9. public class SharedData{ <br /> private String data = "HAPPY"; <br /> public void accessData(){ <br /> for(inti =0; i<20; i++)<br /> { <br /> data = data + " " + i + " "; <br /> } <br />System.out.println(data); <br /> } <br />} <br />Shared data<br />
  10. 10. public class MyThread extends Thread{ <br /> private SharedData data; <br /> public MyThread(SharedData data){ <br /> = data; <br /> } <br /> public void run(){ <br />data.accessData(); <br /> } <br />} <br />Shared data<br />
  11. 11. public class ThreadSynchronizationTest { <br /> public static void main(String[] args) { SharedData data = new SharedData(); MyThread one = new MyThread(data); one.start(); MyThread two = new MyThread(data); two.start(); <br /> } <br />} <br />And the output is??<br />Shared data test<br />
  12. 12. public synchronized void accessData(){ <br /> for(inti =0; i<20; i++)<br /> { <br /> data = data + " " + i + " "; <br /> } <br />System.out.println(data); <br /> } <br />Synchronize<br />
  13. 13. synchronized (this)<br />for(inti =0; i<20; i++)<br /> { <br /> data = data + " " + i + " "; <br /> } <br />System.out.println(data); <br />}<br />Synchronize block<br />
  14. 14. Q&A<br />