18 concurrency

14
Thread Dhrubojyoti Kayal

description

 

Transcript of 18 concurrency

Page 1: 18   concurrency

ThreadDhrubojyoti Kayal

Page 2: 18   concurrency

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

Page 3: 18   concurrency

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

Page 4: 18   concurrency

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?

Page 5: 18   concurrency

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?

Page 6: 18   concurrency

Extend Thread classpublic class MyThread extends Thread {

@Overridepublic 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

Page 7: 18   concurrency

public class MyRunnable implements Runnable {

@Overridepublic 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

Page 8: 18   concurrency

public class MyThread extends Thread {private int max;private int min;public MyThread(int min , int max) {this.max = max;this.min = min;}@Overridepublic 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

Page 9: 18   concurrency

public class SharedData{ private String data = "HAPPY"; public void accessData(){

for(int i =0; i<20; i++){

data = data + " " + i + " "; } System.out.println(data);

} }

Shared data

Page 10: 18   concurrency

public class MyThread extends Thread{ private SharedData data; public MyThread(SharedData data){

this.data = data; } public void run(){

data.accessData(); }

}

Shared data

Page 11: 18   concurrency

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

Page 12: 18   concurrency

public synchronized void accessData(){ for(int i =0; i<20; i++){

data = data + " " + i + " "; } System.out.println(data);

}

Synchronize

Page 13: 18   concurrency

synchronized (this)for(int i =0; i<20; i++)

{ data = data + " " + i + " ";

} System.out.println(data);

}

Synchronize block

Page 14: 18   concurrency

Q&A