Berne University of Applied Sciences E. Benoist / E. Dubuis January 2005 1
Multithreading in Java Java provides the programmer with built-in threading capabilities The programmer can create and manipulate threads; Java provides mechanisms for the cooperation of threads. You use a multithreaded program if your program needs to do more than one thing at a time. For instance, load and process a large image file using a background thread while another thread is responsible for user-input responsibility. Threads - 2
Examples of use of threads Download a file One thread loads the file, the other work Background Task Some task must answer instantly, it starts other threads that do not stop it. Deamons Web server treats requests as threads. It can handle more than one request at a time.... Threads - 3
Sequencial flow of control main MainClass Some Class new amethod some Object JT 1 Threads - 4
Standalone program having two threads main MainClass AThread Class new start athread Object run JT 2 Threads - 5
The construction of Threads Any class can be used to become a starting point for a thread by extending class java.lang.thread by implementing the java.lang.runnable interface must implement the run method After having created a thread, it accepts only a few methods Threads - 6
Extending java.lang.thread import java.lang.thread; public class MyThread extends Thread { // The argument name can be used for debugging public MyThread(String name){ super(name); public void run(){ // Do something here such as creating new objects, // calling methods, perhaps in a never ending loop. // Implicitely called from the start method. Threads - 7
Implementing java.lang.runnable The java.lang.runnable interface public interface java.lang.runnable{ public void run(); Write your class import java.lang.runnable; public class MyOtherThread implements Runnable{... public void run(){ //As for class MyThread above. Threads - 8
Implementing Runnable is the prefered way The class can be a subclass of any other class than class Thread You do not have to take care with interractions with class Thread Synchronized Methods Threads - 9
Starting Threads When Extending java.lang.thread Thread t = new MyThread( aname ); // The thread is started by applying the start method t.start(); When implementing java.lang.runnable // r is the target object of the thread Runnable r = new MyOtherThread(); Thread t = new Thread(r, anothername ); // start the thread t.start; Notice A thread can not be restarted Applying the start() method after termination: java.lang.illegalthreadstateexception Threads - 10
Deamon threads Some threads objects have a deamon status Thread t =...; t.setdeamon(true); t.start(); They are immediately terminated after the last non-deamon thread has terminated Threads - 11
Thread Priorities Threads are held in priority-based scheduling queues By default, each thread has the priority of the one that created it Priority can be changed: setpriority(). It must stay within Thread.MIN_PRIORITY and Thread.MAX_PRIORITY Threads with higher priority are executed first Priority can be a substitute for locking Threads - 12
What do we want Threads to do? Be created Thread t = new Tread(r, anyname ); t.start; Access the current Thread (since this is not a thread) Thread.currentThread Sleep durring some time Thread.sleep(10000);// 10000 milliseconds Wait for a ressource to be free Let other threads to possibility to be executed Thread.yield()... Threads - 13
Problems with Treads Accessing the memory A value can be accessed simultaneously by many threads If they increment it how many increment will be done? Access and updates of memory cells are: not atomic for long and double atomic (can not be devided) for any other type long and double can be atomic if defined as volatile: public class Measurment { private volatile long index; private volatile double value; Some methods should only be used by one only Thread Threads - 14
Thread Life Cycle I new JT 3 New Dead start exit run yield sleep Runnable time el., interrupt wait notify, notifyall, interrupt blocking, locking, join unblocking, unlocking, return from join interrupt Not Runnable Threads - 15
Stopping Threads Stopping Threads Using Some Variable public class SomeThread extends Thread{ private volatile boolean done; //... public void run(){ // perhaps make some initialization first while (! done) { // do something // Perhaps do some cleaning up Threads - 16
Stopping Threads Using Some Variable The place where the checking of the condition is performed repeatedly guarantees that the thread cannot hold any lock at this place. Thus, if the thread is about to stop, then it is guaranteed that It does not hold any lock and thus has left any sychnonized methods How to change the value a method for falsifying the condition // In SomeThread class public void terminate(){ done = true; Threads - 17
Thread.interrupt We call this method on a target thread: // In the body of some method Runnable r =... Thread t = new Thread(r); // then later t.interrupt() Threads - 18
Thread.interrupt II Effect if the thread is runnable Its interruption status is set to true The thread has to check its interruption status periodically public class SomeThread implements Runnable{ public void run(){ // perhaps make some initialization first. while(! Thread.currentThread().isInterrupted()){ // Do something // perhaps do some cleaning Threads - 19
Thread.interrupt III If the thread is blocked on methods: Object.wait, Thread.sleep, Thread.join the actions throw an InterruptedException and t s interruption status is set to false public class SomeThread implents Runnable{ public void run(){ while( cond &&!Thread.currentThread().isInterrupted()){ //Do something then try{ Thread.sleep(10000); catch (InterruptedException e){ //Perhaps do some cleaning up return; // Cond becomes false after a while... Threads - 20
Mutual Exclusion of Threads Let us consider 2 threads incrementing an object (implementing interface IntContainer ) :IntContainer incr() incr() incr1 incr1 Threads - 21
IntContainers Interface and Implementations Interface can have many implementations having different behaviours public interface IntContainer { public void incr(); public void read(); Very easy class class IntContainer1 implements Container { private int n=0; public void incr() { n++; public int read() { return n; Threads - 22
A thread incrementing the object public class Incr implements Runnable { private int m; private IntContainer n; public Incr(IntContainer n, int m){ this.m = m; this.n = n; public void run() { for (int i = 0; i < m; i++) { n.incr(); Threads - 23
Let s create two threads and start them IncContainer n = new IntContainer1(); Thread incr1 = new Thread(new Incr(n,m)); Thread incr2 = new Thread(new Incr(n,m)); incr1.start(); incr2.start(); try { incr2.join(); incr1.joint(); catch(interruptedexception e) { return; if (n.read() == 2 m) { System.out.println( Value n is Correct: + n.read() +. ); else { System.out.println( Value n is too SMALL: + n.read() +, it should be: + 2 m +. ); Threads - 24
Let s create two threads and start them Output is something like: > java Increment1 10000 Value n is too SMALL: 10745, it should be: 20000. Threads - 25
Reason Thread incr1 Thread incr2 Case: n = 60 LOAD n The value 60 is now in the register of this thread this thread does something else ADD 1 Register contains 61 LOAD n the value 60 is in the register of this thread STORE n the value 61 is stored in ADD1 register contains 61 memory location n this thread does something else STORE n the value 61 is stored in memory location n, the old value, 61, is overridden n = 61, instead of 62 Threads - 26
Volatile variables may help? The introduction of the volatile modifier does not help either class IntContainer2 implements IntContainer { private volatile int n = 0 ; public void incr() { n++; // n++ operation is not atomic public int read() { return n; Threads - 27
Next Improvement incr() should not be executed by more than one thread It is the critical section. There must be mutual exclusion in the body of incr() The synchrinized modifier of methods public synchronized void incr(){ n++; Corresponds to the following pseudo-code // Body of incr, Acquire lock or wait until lock is available; n++ Release lock; A thread remains there until the lock becomes available. Upon unlocking an object by a thread, all other threads wanting the lock of that particular object are put back into the Runnable state, ready for thread scheduling. Threads - 28
When Using synchronized Methods Let s modify the integer container class IntContainer3 implements IntContainer { private int n; public synchronized void incr(){ n++; public int read() { return n; Output: > java Increment 10000 Value n is CORRECT: 20000 Threads - 29
Remarks on IntIncrement3 read returns the current value of the int Synchronization is not necessary since read of int value is atomic. If the value were long then read should either be synchronized, or the field should be volatile. If the value were an array or an object composition, then a synchronized would be mandatory. Threads - 30
Further Notes on Synchronization It is sometime more performing to acquire the lock for only a part of the body of a method: // Some method s body... synchronized(this) { other statements;... Or, the exclusive access of another object is necessary within a statement sequence: // Some method s body... synchronized(anotherobject) { anotherobject.somemethod();...... Threads - 31
Java monitors are reentrant A synchronized method can be called from within a synchronized method Once acquired the lock, a thread calling another method of the already locked object does not block. Two methods of the class: public synchronized void a() { this.b(); // Does not block, has lock already public synchronized void b() {... Threads - 32
Notes on Synchronization Non-sychronized methods can be executed by any thread at any time class X { void a() {... synchronized void b() {... synchronized void c() {... (a,b) and (a,c) can be executed non-exclusively! However (b,c) is not possible! Execution penalty when using synchronization Synchronizing methods makes the program safier It is a costly operation Threads - 33
Conditionally synchronizing threads I Threads must cooperate To let them cooperate, shared objects are used The progress of a thread may depend on the progress of others One key abstraction often used in such a context is the one of a Buffer public abstract class Buffer { public void put(object x) throws InterruptedException; public Object get() throws InterruptedException; Threads - 34
Conditionally synchronizing threads II Considere a Producer/Consumer relation Producer: public class Producer implements Runnable { private Buffer buffer; public void run() { Integer item = null; for (int i = 0; i < 10; i++) { // Compute new item. buffer.put(item); Threads - 35
Conditionally synchronizing threads III Consumer public class Consumer implements Runnable { private Buffer buffer; public void run() { Integer item = null; for (int i = 0; i < 10; i++) { item = (Integer) buffer.get(); // Do something, e.g., print the value of the item Threads - 36
Conditionally synchronizing threads IV Both threads are using a OnePlaceBuffer1 class OnePlaceBuffer1 extends Buffer { private Object content; public synchronized void put(object x) throws InterruptedException { content = x; // Pint statements for testing purpose public synchronized Object get() throws InterruptedException { return content; // Print statement for testing purpose Threads - 37
Tests Creating a OnePlaceBuffer1 object and the two threads (n=5) > java test.oneplacebuffertest1 5 consumes: null, Produces: 0, Produces: 1, consumes: 1, consumes: 1, Produces: 2, consumes: 2, consumes: 2, Produces: 3, consumes: 3, Produces: 4 Remarks null has been fetched before something has been produced 1 has been stored before 0 has been fetched 4 has never been consumed There must be a way to coordinate threads! Threads - 38
Waiting on Conditions to Happen Slow down the Producer or the Consumer to work together // Pseudo java private boolean empty = true; public synchronized void put(object x) throws InterruptedException { while (!empty) { wait until the value has been consumed empty = false; contents = x; public synchronized Object get() throws InterruptedException { while(empty) { wait until a value has been produced; empty = true; return contents; Threads - 39
1.4 Threads - 40
slidesthreads.tex Threads - 41
======= Coordinate threads This is called condition synchronization How should the bodies of the while loops be implemented? Specific methods of the class Object. wait() The current thread releases the lock of this object and waits (going into thread state NotRunnable). The thread is added to the object s wait set. It waits there until being notified by another thread. wait(millisec) and wait(millisec, nanosecs) The current thread releases the lock of this object and waits (going to the state NotRunnable). The thread is added to the object s wait set. It waits there until being notified by another thread, or the time-out period has elapsed. The second form allows finer control for specifying the time to wait. Every object has an associated wait set. Threads - 42
Coordinate threads (Cont.) private boolean empty = true; public synchronized void put(object x) throws InterruptedException { while (!empty) { try{ this.wait(); catch (InterruptedException e){ throw e; empty = false; content = x; public synchronized Object get() throws InterruptedException { while(empty) { try{ this.wait(); catch (InterruptedException e){ throw e; empty = true; return content; Threads - 43
Dead Lock Output > java test/oneplacebuffertest2 Produces: 0 Produces: 1 ˆC Observations of this particular run 1. Producer produces item 0 and puts it into the buffer. 2. Producer produces item 1 then it blocks. 3. Consumer consumes item 0. 4. Consumer wants to consumed item 1 then it blocks. 5. Both threads are waiting. This is a Dead Lock Threads - 44
Notifying another thread to Unlock from the Wait Set The previous version Dead Locks. There is no thread which notifies any of the threads once added to the wait set. The notify family of methods notify() The current threads wakes up an arbitrary, single thread That thread enters the Runnable state It is ready for thread scheduling (of course it can not go one if the current thread does not relinquishes the object s lock) notifyall() wakes up all the threads of the wait set they become Runnable and are ready for scheduling. Threads - 45
Notifying To fix our problem, every thread notifies the other to resume. private boolean empty = true; public synchronized void put(object x) throws InterruptedException{ while (! empty){ try { this.wait(); catch (InterruptedException e) { throw e; empty = false; content = x; this.notify(); Threads - 46
Notify for get Similarly, for the get method public synchronized Object get() throws InterruptedException{ while (empty){ try { this.wait(); catch (InterruptedException e){ throw e; empty=true; this.notify(); return content; Threads - 47
Output > java test.oneplacebuffertest3 Produces: 0 consumes: 0 Produces: 1 consumes: 1 Produces: 2 consumes: 2 Produces: 3 consumes: 3 Produces: 4 consumes: 4 It works! Threads - 48
When having n Producers and Consumers 10 producers and 10 consumers each handling 5 items A particular run could be >java OnePlaceBufferTest4 prodcnt 10 conscnt 10 5 Produces: 0, consumes: 0, Produces: 0, consumes: 0 Produces: 0,... consumes: 3, Produces: 2, consumes: 2, Produces: 2 ˆC It sometimes deadlocks Suppose : one producer and 10 consumer in the wait set Another producer produces an item and stores it It makes a notify but unfortunately wakes up the only producer waiting The buffer is full and all consumers are waiting Dead Lock! Threads - 49
Improvement using a notifyall() I private boolean empty = true; public synchronized void put(object x) throws InterruptedException{ while (! empty){ try { this.wait(); catch (InterruptedException e) { throw e; empty = false; content = x; this.notifyall(); Threads - 50
Improvement using a notifyall() II public synchronized Object get() throws InterruptedException{ while (empty){ try { this.wait(); catch (InterruptedException e){ throw e; empty=true; this.notifyall(); return content; Threads - 51
Output Having 10 producers and 10 consumers, each handling 5 items >java OnePlaceBufferTest4 prodcnt 10 conscnt 10 5 Produces: 0 consumes: 0 Produces: 0 consumes: 0 Produces: 0... consumes: 4 Produces: 4 consumes: 4 Threads - 52
Suspending Threads There are two methods for stopping and restarting threads suspend() resume() What is the problem with Thread.suspend the thread immediately suspends its execution It does not release any of the locks it holds Other threads are still blocked Both suspend() and resume() are deprecated. The programmer should introduce variables and check them repeatedly We use a variable suspended When it is true, we wait() The thread is resumed when it receves a notify() Threads - 53
Suspending Threads : The Safe Way // In some class extending Thread or implementing Runnable private boolean suspended; public void run() { while (continue) { // The following must be in a try clause. try { // check first if thread can continue. if (suspended) { synchronized (this) { while (suspended) { this.wait(); catch (InterruptedException ex) { return; // Do something usefull here. Threads - 54
Suspending Threads : The Safe Way II We need methods to set the value of suspended public void hold() { suspended = true; public synchronized void release(){ suspended = false; this.notify(); 1.4 Java requires that notify() (and notifyall()) be only in synchronized methods or blocks. It avoids race conditions whcih can occure if notifications can occur sumultaneously with the application of wait methods. Threads - 55
Bibliography Doug Lea, Concurent Programming in Java, second edition, Addison Wesley, 2000, ISBN 0-201-31009-0. See also: http://gee.cs.oswego.edu/dl/cpj Joshua Block, Effective Java Programming Language Guide, Addison Wesley, 2001, ISBN 0-2001-31005-8. Why are Thread.stop, Thread.suspend, Thread.resume and Runtime.runFinalizersOnExit Depracated?, http://java.sun.com/j2se/1.4/docs/guide/misc/ threadprimitivedeprecation.html Threads - 56