18 concurrency
-
Upload
dhrubo-kayal -
Category
Documents
-
view
945 -
download
0
description
Transcript of 18 concurrency
ThreadDhrubojyoti Kayal
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 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
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
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
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
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(int i =0; i<20; i++){
data = data + " " + i + " "; } System.out.println(data);
}
Synchronize
synchronized (this)for(int i =0; i<20; i++)
{ data = data + " " + i + " ";
} System.out.println(data);
}
Synchronize block
Q&A