You are on page 1of 19


Taming Java Threads

What We’ll Do Today

•Programming threads in Java is
fraught with peril, but is mandatory in
a realistic program.

Allen I. Holub
Holub Associates

•This talk discusses traps and
pitfalls, along with some solutions

•This talk focuses on material not
covered in most books



Taming Java Threads, (c) 2002 Allen I Holub <>



Taming Java Threads, (c) 2002 Allen I Holub <>

Who is the Guy?

I'm assuming that...

Talk based on my JavaWorld™ “Java Toolbox” column,
now a book:
– Taming Java™ Threads, (Berkeley: APress, 2000;

• Source code, etc., found at
• My Prejudices and Bias
– I do not work for Sun
– I have opinions and plan to express them. The appearance of
impartiality is always just appearance
– Java is the best thing since sliced bread (but bakery bread is
better than sliced).



Taming Java Threads, (c) 2002 Allen I Holub <>

• You may still get something out of the talk if you
don't have the background, but you'll have to



Taming Java Threads, (c) 2002 Allen I Holub <>

We’ll look at

Books, etc.

• Thread creation/destruction problems

• Platform-dependence issues

• Synchronization & Semaphores
(synchronized, wait, notify, etc.)

Allen Holub, Taming Java™ Threads. Berkeley, APress,
Doug Lea. Concurrent Programming in Java™: Design
Principles and Patterns, 2nd Ed.:. Reading: Addison
Wesley, 2000.
Scott Oaks and Henry Wong. Java™ Threads.
Sebastopol, Calif.: O'Reilly, 1997.
Bill Lewis and Daniel J. Berg. Threads Primer: A Guide
to Multithreaded Programming. Englewood Cliffs:
Prentice Hall/SunSoft Press, 1996.
technicalArticles /Threads/

• Memory Barriers and SMP problems
• Lots of other traps and pitfalls

• A catalog of class-based solutions

• An OO- based architectural solution



I'm assuming you know:
– the language, including inner classes.
– how to create threads using Thread and
– synchronized, wait(), notify()
– the methods of the Thread class.

Taming Java Threads, (c) 2002 Allen I Holub <>



Taming Java Threads, (c) 2002 Allen I Holub <>

Taming Java Threads, (c) 2002 Allen I Holub <>


Thread. ™ – Threads share the process’s memory – Thread context swaps are much lower overhead than process context swaps It's not okay to have an unresponsive UI. HPUX.MIN_PRIORITY) A Thread must voluntarily relinquish control of the CPU.> • Different operating systems use different threading models (more in a moment).holub. (c) 2002 Allen I Holub <www.MAX_PRIORITY. Fast context swap.holub. Posix. – Stick to> Threading models • The Java™ programming language has 10 levels • – but they're worthless---there are no guarantees that the OS will pay any attention to them. • Multithreaded apps can be slower than single-threaded apps (but be better organized) Middle 10 Priorities Taming Java Threads. Middle 11 Taming Java Threads.NORM_PRIORITY. Slower context swap. (c) 2002 Allen I Holub <www. but easier to program and multiple threads can run on multiple processors.) – – After certain (unspecified) I/O operations priority is boosted (by an indeterminate amount) for some (unspecified) time. concurrency ) A process is a JVM instance.03/06/2003 Words to live by Threads vs.holub. Taming Java Threads. processes in Java • • • You need to use the OS threading system to get parallelism (vs. (c) 2002 Allen I Holub <www. (c) 2002 Allen I Holub <www. • A Thread is a flow of control through that address space.holub. Cooperative (Windows 3. but hard to program and can’t leverage multiple processors. • A Process is an address space. Middle 7 Taming Java Threads. (c) 2002 Allen I Holub <www. processes All nontrivial applications for the Java platform are multithreaded. Etc. – Most code in books isn't thread safe Taming Java Threads. (c) 2002 Allen I Holub <> Taming Java Threads. It’s not okay for a server to reject> Middle 12 Control is taken away from the thread at effectively random times. memory) shared with other threads or be reentrant. (c) 2002 Allen I Holub <www.1) – – • The Solaris™ OS has 231 levels • NT™ offers 5 (sliding) levels within 5 "priority> Middle 8 Thread behavior is platform dependent! Threads vs. whether you like it or> • Behavior often based on timing. Hybrid (Solaris™ OS. – The Process contains the heap (everything that comes from new) – The heap holds all static memory A thread is a runtime (JVM) state – The "Java Stack" (runtime stack) – Stored registers – Local variables – Instruction pointer • Middle 9 Taming Java Threads. Thread-safe code can run in a multithreaded environment – Must synchronize access to resources ( eg." • Preemptive (NT) – – • • NT priorities change by> 2 .holub. Simultaneous cooperative and preemptive models are supported.holub.

} public foo() { // This method will not run on the thread since // it isn’t called by run() } } Middle 17 Taming Java Threads. methods> Do not assume a particular environment Thread creation • Assume both of these rules.holub.> Java™ threads aren’t object oriented (2) • Simply putting a method in a Thread derivative does not cause that method to run on the thread.holub.03/06/2003 NT™ threading model Solaris™ OS threading model (Win32 "fibers" are so poorly documented. Thread thread_controller=new Thread(new Operation()). (c) 2002 Allen I Holub <> –They execute the same code with the same this reference. (c) 2002 Allen I Holub <www. they are not a real option. (c) 2002 Allen I Holub <www.A thread can prevent other threads from running if it doesn't occasionally yield – It’s a thread controller – by calling yield(). all the time: • Java’s Thread class isn’t (a thread). so share the object’s state. 1.start(). performing a blocking I/O> 3 . (c) 2002 Allen I Holub <www. (c) 2002 Allen I Holub <www.holub.) Middle 13 Taming Java Threads. Middle 18 Taming Java Threads. class Operation implements Runnable { public void run() { // This method (and the methods it calls) are // the only ones that run on the thread. etc.holub. • Objects do not run on threads.holub.holub. and so buggy. • Several threads can send messages to the same object> Taming Java Threads. } } 2. (c) 2002 Allen I Holub <www. Middle 15 Taming Java> Middle 14 Taming Java> Middle Java™ threads aren’t object oriented (1) 16 Taming Java Threads.A thread can be preempted at any time by another thread – even by one that appears to be lower priority than the current one. – A method runs on a thread only if it is called from run()(directly or indirectly).holub. (c) 2002 Allen I Holub <www. class Fred extends Thread { public void run() { // This method (and the methods it calls) are // the only ones that run on the thread.

0x0000000089abcdef. synchronized(lock) { field = new_value } Middle 24 Taming Java Threads. (c) 2002 Allen I Holub <www. • Think “airplane bathroom” – Only one person (thread) can be in it at a time (we hope). but is poorly designed. • Every Object contains an internal mutex: Object mutex = new Object(). Taming Java Threads. (c) 2002 Allen I Holub <www. – Though it is sometimes called a “lock. – Do not run simultaneously when accessing the same object ("monitors" implemented with A"mutex"). (c) 2002 Allen I Holub <www. a thread must have the key.03/06/2003 Basic concepts: atomic operations (atomicity). 0x0000000000000000.holub. as is the Class object.holub.high = 0x01234567 x. (c) 2002 Allen I Holub <> Middle 20 The mutex (mutual-exclusion semaphore) Basic concepts: semaphores • • Middle 21 • The mutex is the key to a lock A semaphore is any object that two threads can use to synchronize with one> – Arrays are also objects. – Only one thread can have the key (own the mutex) at a time. } Resist the temptation to use a Java native interface (JNI) call to access the underlying OS synchronization mechanisms. Middle 19 x. long x . • Entering the monitor does not restrict access to objects used inside the monitor—it just prevents other threads from entering the monitor.low = 0x89abcdef. – Don't be confused by Microsoft™ documentation that (incorrectly) applies the word "semaphore" only to a Dijkstra counting semaphore. 0x0123456700000000. • The synchronized keyword is essential in implementing synchronization. Taming Java Threads. – Do not run simultaneously when accessing the same code ("critical sections").com> Taming Java Threads. (c) 2002 Allen I Holub <www. Object lock = new Object().com> long> Taming Java Threads.holub. – Every object has its own monitor (and its own mutex).holub. You can't leave without unlocking the door.g.holub. Middle 22 • Monitors create atomicity by using mutual exclusion semaphores. (c) 2002 Allen I Holub <www. • • Basic concepts: synchronization Atomic operations can't be interrupted (divided) Assignment to double or long is not atomic • Mechanisms to assure that multiple threads: – Start execution at the same time and run concurrently ("condition variables" or "events"). possible results: 0x0123456789abcdef. No timeout. You can be preempted between the assignment> Synchronization with individual locks Monitors and airplane bathrooms Middle Taming Java Threads. – e. 64-bit assignment is thread 1: effectively implemented x = 0x0123456789abcdef as: thread 2: x = 0. 23 Taming Java Threads. (c) 2002 Allen I Holub <www. otherwise it blocks (is suspended). access to which is guarded by a single mutex. – Acquisition is not necessarily FIFO order. • A monitoris a body of code (not necessarily contiguous). synchronized( mutex ) { // guarded code is here. so deadlock detection is> 4 . • Enter the monitor by passing over the synchronized keyword (acquire the mutex). – Locking the door acquires the associated mutex.holub. – Other people must line up outside the door if somebody's in there.holub.” • Ownership is the critical concept – To cross a synchronized statement.

(c) 2002 Allen I Holub <www. } } Middle 26 Constructors can’t be> Synchronizing the inner-class method doesn't work Be careful to lock the correct object Middle Taming Java Threads. (c) 2002 Allen I Holub <www. (c) 2002 Allen I Holub <www. class Unpredictable { private final int x. } }. } } } public Unpredictable(int init_x. private JButton b = new JButton(). (It's a silent no-op. } public void race_condition(double new_value) { d => Taming Java Threads.addActionListener ( new ActionListener() { synchronized // grabs the wrong lock! public void actionPerformed(ActionEvent e) { d = 0. // race condition! } } ). } } • synchronized(this) does not work in a> Taming Java Threads.g. so always have back> 5 . • The monitor is associated with the object.addActionListener ( new ActionListener() { public void actionPerformed(ActionEvent e) { d => Locking the constructor’s back door.) • Putting the thread-creation code at the bottom doesn’t help (the optimizer might move it). public Predictable(int init_x. } public void race_condition(double new_value) { d = new_value. int init_y) { synchronized( lock ) { new Thread() { public void run() { synchronized( lock ) { // Use shared var } } }.holub.03/06/2003 Method-level synchronization He came in the Bathroom Window.println(“x=“ + x + “ y=“ + y). class Queue { public synchronized void enqueue(Object o) { /*…*/ } public synchronized Object dequeue() { /*…*/ } } • The Bathroom can have several doors • Acquiring a lock on an object does not prevent other threads from modifying that object.0. – E. not the code.holub. class Predictable { Object lock = new Object().com> public synchronized void ringo(double some_value) { guard_this = some_value.start(). y = init_y. public Outer() { b. x = init_x.holub.holub. (c) 2002 Allen I Holub <www. private final int y. } } ). (c) 2002 Allen I Holub <www. class Bathroom_window { private double guard_this. – Two threads can happily access the same synchronized code at the same time. (c) 2002 Allen I Holub <www. } } 29 Taming Java Threads. //initialize shared var.holub.start(). (c) 2002 Allen I Holub <www.out. int init_y) { new Thread() { public void run() { System. Two threads can enqueue to different queues at the same> Middle 28 • An inner-class event handler is also a back door class Outer { private double d. Middle // WRONG! Needs // synchronization Middle 30 Taming Java Threads. provided that different objects receive the request. private JButton b = new JButton(). but they cannot simultaneously access the same queue: – Same as synchronized(this) Middle 25 Taming Java Threads.holub.0. 27 Taming Java Threads. } public double george() { return guard_this.holub. public Outer() { b. } } class Outer { private double d.

0.39% increase 139 ms.clone() ). (HotSpot is okay.87% increase 155 ms. • Consider having a synchronized non-reentrant public method call a reentrant private method. – Code that uses only local variables and arguments (no static variables. 1.) tester.) – Assignment to all non-64-bit things.holub. NT4/SP3. (c) 2002 Allen I Holub <www.} Middle 36 Taming Java Threads. 155.0).com> class Synch { synchronized int locking ( int a. (c) 2002 Allen I Holub <www.0fcs. • Contention in last two passes (Java Hotspot can’t use atomic-bit-test-and-set). int { return a + not_locking ( int a. --i >= 0 .484. • Overhead can be insignificant when the synchronized method is doing a time-consuming operation. 33 Taming Java Threads. 155.87% increase 157 ms. double end = new Date(). (c) 2002 Allen I Holub <www.70% increase 4. (c) 2002 Allen I Holub <www.52% increase 3. Synchronization isn’t cheap class Outer { private double d.> Taming Java Threads.1. small synchronized methods often chain to small synchronized methods. } private void workhorse( long some_field ) { // no fields of class are used in here.getTime()> 6 . (c) 2002 Allen I Holub <www.get Time().addActionListener ( new ActionListener() { public void actionPerformed(ActionEvent e) { synchronized( Outer. JDK 1. (c) 2002 Allen I Holub <www. public synchronized void accessor() { workhorse( some_field. – Code may be reordered. } } Middle 31 Taming Java Threads.> Taming Java> Volatile • Reentrant code doesn’t need to be synchronized. E Middle b ) b. Middle Taming Java Threads.holub. including booleansand references are usually safe. 121.2.locking(0. public Outer() { b.891 ms.> Synchronization isn’t cheap 200MHz Pentium.start. HotSpot 1.29% increase 156 ms. – Assignment to volatile doubles and floats should be atomic (but most JVMsdon’t do it). – volatile might not work in all JVMs. Object some_field = new Some_class(). int for(long i = 1000000.} b ) b.668. } synchronized public void race_condition(double new_value) { d = new_value.407 ms. int { return a + static public void main(String[] arguments) { double start = new Date().holub. private JButton b = new JButton(). – But in OO systems. so assignment to several atomic variables must be synchronized. } } } ). though. 154. – used values are stale.this ) { d = 0.33% increase BUT • The cost of stupidity is always higher than the cost of synchronization. 155.96% increase 156 ms. (c) 2002 Allen I Holub <www.holub. (Bill Pugh) – Pick a fast algorithm. } 35 Taming Java Threads.52% increase 157 ms. double locking_time = end .com> Middle 34 Reentrant Code • Atomic operations on volatile primitive types often do not need to be synchronized.03/06/2003 Explicitly synchronize on the object that holds the contested fields.holub. // repeat for not_locking } } Middle 32 Synchronization isn’t cheap % java -verbose: gc Pass 0: Time lost: Pass 1: Time lost: Pass 2: Time lost: Pass 3: Time lost: Pass 4: Time lost: Pass 5: Time lost: Pass 6: Time lost: Pass 7: Time lost: Pass 8: Time lost: Synch 234 ms. no fields in the class).

com> Taming Java Threads. • Don't synchronize on read-only access.: Avoid Vector and Hashtable in favor of Collection and Map derivatives. • Avoid heavy use of BufferedInputStream. too. many-reader strategies are best. Collection c = Collections. t0. Collection c = new ArrayList (). (c) 2002 Allen I Holub <www. (c) 2002 Allen I Holub <www. s1 = t0.toString(). • Synchronize the smallest block possible to minimize the odds of contention.*/} } • The only solution is not to use string operations or StringBuffers! Middle 41 Taming Java Threads. Taming Java Threads.holub. but Vector and Hashtable objects are usually used from within synchronized methods. (c) 2002 Allen I Holub <www. • The StringBuffer class's append() method is synchronized! • synchronized is not part of the signature – This is a problem with public> Middle 38 Don't Nest Synchronization Taming Java> Don't use Buffered Streams • Don’t access synchronized methods from synchronized methods. – Collections and Maps accessors are not synchronized. BufferedReader .com> class Bar extends Foo { protected void f() {/*.com> 7 .. is really Stringbuffer t0 = new StringBuffer(s2). Middle 39 Taming Java Threads. – Single-byte access is synchronized! • How often do multiple threads simultaneously access the same stream at the byte level? – You might use write(byte[]). – Method-level synchronization should be avoided in very-high-performance systems. • E. Middle 37 Taming Java Threads.holub. public class Foo { protected synchronized void f(){/*.holub.holub.holub. – Works if a single writer is simply incrementing (but the change might not be immediately visible).com> Don't use protected • No guarantee that derived classes correctly synchronize access to protected fields. Don’t synchronize private ones. c = Collections. – Vector and Hashtable access is synchronized. – But a change of state might not be immediately visible to other> • Best to roll your own version of BufferedOutputStream that’s not synchronized. etc. (c) 2002 Allen I Holub <www... (c) 2002 Allen I Holub <www. – No guarantee that derived -class overrides synchronize properly: • String concatenation uses a StringBuffer: s1 = s2 + s3. (c) 2002 Allen I Holub <www.append( s3 ). – Will not work if multiple threads perform updates. – You can copy the source and rename the class Middle 40 Avoid String Concatentation and StringBuffer Objects.03/06/2003 Using Volatile Safely Synchronization Rules of Thumb • One-writer.synchronizedCollection(c).holub. • Assignment to non-Boolean is risky. – The value might change at surprising times. (c) 2002 Allen I Holub <www.synchronizedCollection(c). • Don’t synchronize the methods of classes that are called only from one thread. BufferedOutputStream.*/} } Middle 42 // AAGH! Taming Java Threads. read(byte[]).holub. – Use Collection-style synchronization decorators when you need synchronized behavior. • Do not depend on the "current" value of a volatile. or BufferedWriter – Synchronize public methods.

– Language has no notion of “constant”. – The class (static) methods handle messages sent to the class as a whole.g. (c) 2002 Allen I Holub <www. long x => Critical sections can also synchronize on the class object Critical sections Middle Immutable ≠ constant (but it must be constant to be thread safe) – A final reference is constant. Results are undefined.set_x(-1). (c) 2002 Allen I Holub <www. synchronized( critical_section ) { // only one thread at a time // can execute this code } 45 Taming Java> Middle 44 • A critical section is a body of code that only one thread can enter at a time. • Think class variables vs. – Could be disastrous if they share references.holub. class I_am_immutable { private final int some_field.holub. – The instance (non-static) methods handle messages sent to the individual objects.holub. instance variables: – The class (static) variables and methods are effectively members of the Class object.x = x.get_x(). – The instance (non-static) variables store the state of the individual objects. but the referenced object can change state. } synchronized /* not static */ double get_x() { return x.) Middle 48 Taming Java Threads.class ) { // only one thread at a time // can execute this code } } public static synchronized void wilma() { // synchronizes on the same object // as fred().holub. (c) 2002 Allen I Holub <www. compiler bug) • Middle 43 Taming Java Threads. synchronized static void set_x( long x ) { Foo. • Do not confuse a critical section with a> Taming Java Threads. – Blank finals must be initialized in all constructors. one on the class object and one on the instance. } } – Might not compile with inner classes (there’s a long-standing • May run while objects referenced by fields are still in use! • Two different objects may be finalized> } Middle 46 class Foo { static long x = 0. 47 Taming Java> 8 .com> But remember the bathroom with multiple doors Class vs. – The monitor is associated with an object – A critical section guards code • The easiest way to create a critical section is by synchronizing on a static field: class Flintstone { public void fred() { synchronized( Flintstone. (c) 2002 Allen I Holub <www. • All fields of the object are final (e. (There are two locks here. (c) 2002 Allen I Holub <www. public I_am_immutable( int initial_value ) { some_field = initial_value. – Blank finals are final fields without initializers. (c) 2002 Allen I Holub <www. (c) 2002 Allen I Holub <www.03/06/2003 Don't Use Finalizers Do use Immutable objects • Synchronization not required (all access read -only).com> Taming Java Threads. Thread 2: Foo.holub. so you must guarantee it by hand Thread 1: Foo o1 = new Foo(). – The class (static) variables store the state of the class as a whole.holub.holub. } } • All synchronized static methods synchronize on the same monitor. } static final Object critical_section = new Object(). instance variables Middle Taming Java Threads. String) • They slow down the garbage collector.

public final class Singleton { static { new JDK_11_unloading_bug_fix(Singleton. (c) 2002 Allen I Holub <> Or alternatively… • Thread safe because VM loads only one class at a time and method can’t be called until class is fully loaded and initialized. } } Singleton s = Singleton. //. (c) 2002 Allen I Holub <www. public void foo(long x) { //. class Okay { private static long unsafe. • A degraded> public synchronized static Singleton instance() { if( instance == null ) instance = new Singleton(). .} static synchronized set(long x) => Middle 54 Taming Java Threads.holub. } private Singleton(){} • No way to control constructor arguments at run time. public final class Singleton { static { new JDK_11_unloading_bug_fix(Singleton.. } } } Middle 53 Taming Java Threads. } public static Singleton instance() { return instance.holub. (c) 2002 Allen I Holub <www. private Singleton(){} private static final Singleton instance = new Singleton().holub. avoids synchronization. . private Singleton(){} // prevent creation by new class Okay { private static class Class_Variables { private long unsafe.holub.holub. synchronized( Okay.class).holub.class). (c) 2002 Allen I Holub <www. • Singletons often use critical sections for> 9 .com> Middle 50 Lock the extra doors Singletons (one-of-a-kind objects) 3. public foo(long x) { statics.03/06/2003 Lock the extra doors Lock the extra doors 1.} public /*not static*/ void foo(long x) { //.com> Taming Java Threads. } public /*unsynchronized*/ static Singleton instance() { return instance. public synchronized void do_something(long x) { unsafe = x. even if the accessor is a method of the class that contains the field. (c) 2002 Allen I Holub <www. } } Middle 51 Taming Java Threads. } private static Singleton instance. } } static Class_Variables statics = new Class_Variables().instance() Middle 52 Avoiding sychronization in a singleton by using static Taming Java> Taming Java Threads. Synchronize explicitly on the class object when accessing a static field from an instance method. (c) 2002 Allen I Holub <www... } } } class Okay { private private {return private {unsafe static long unsafe. Encapsulate all static fields in an inner class and provide exclusive access through synchronized methods of the inner class. } } Middle 49 Taming Java Threads. (c) 2002 Allen I Holub <www. set(x). 2..holub.do_something( x ). static{ instance = new Singleton(). Access all static fields through synchronized static methods. return instance. static synchronized get() unsafe. public final class Singleton { private static Singleton instance.class ) { unsafe = x.

via a local-variable or t. // (NOT AN IF) } catch( InterruptedException e ) { return null. Middle 56 Notifying_queue(): wait().startsWith("1.wait(). objects not accessible> Taming Java Threads. not all monitors that were acquired along the way (nested monitor lockout). – In the Java™ environment. private int tail = 0.holub. • wait(timeout) does not tell you if it returned because of a timeout or because the wait was satisfied (hard to solve). private int head = 0. // wait abandoned } return queue[++tail %= queue_size ]. and spin locks wait and notify have problems.03/06/2003 Condition variables While we’re on the subject… public class JDK_11_unloading_bug_fix { public JDK_11_unloading_bug_fix(final Class keep) { if (System.holub. (c) 2002 Allen I Holub <www. wait is not atomic (1) Middle 59 Taming Java Threads. • There's no way to test state before waiting. } }58 Taming Java Threads.holub. } public Object synchronized dequeue( ) { try { while( head == tail) //<-. (c) 2002 Allen I Holub <> Condition variables. this. argument were subject to } garbage collection } } Middle 55 Taming Java> Taming Java Threads. (c) 2002 Allen I Holub <> 10 . (c) 2002 Allen I Holub <www.notify().holub. wait is not atomic (2) Middle 60 Taming Java Threads.start().holub.} catch(InterruptedException e){} } In the 1. (c) 2002 Allen I Holub <www.setDaemon(true). • wait() releases only one> • All objects have a "condition variable" in addition to a mutex. public void synchronized enqueue( Object item ) { queue[++head %= queue_size] = item. (c) 2002 Allen I Holub <> Middle class Notifying_queue { private static final queue_size = 10. • Implicit condition variables don't stay set! – A thread that comes along after the notify() has been issued blocks until the next notify(). private Object[] queue = new Object[queue_size].version") . – A thread blocks on a condition variable until the condition becomes true.1") ) { Thread t = new Thread() { public void run() { Class singleton_class = keep.1 JDK™ All } }.com> Condition variables.MUST BE A WHILE this.holub. conditions are "pulsed" — condition reverts to false immediately after waiting threads are released. Middle 57 Taming Java Threads.holub. (c) 2002 Allen I Holub <www.getProperty("java. synchronized(this) { try{ wait(). notify(). • wait() and notify() use this condition variable.> 11 .holub.holub.03/06/2003 Condition variables. wait is not atomic (3) Middle 61 Taming Java Threads. • It may take time for a change made by one thread to become visible to another thread – Threads are running on different processors. (c) 2002 Allen I Holub <www. Middle 65 Taming Java Threads. (c) 2002 Allen I Holub <> Condition> CPU1 w rwwr w CPU2 ww r r w r MU1 memory Middle 66 MU2 Taming Java Threads. wait is not atomic (4) Middle 62 Condition variables. • Changes made by a CPU are not transferred from cache to the main memory store immediately. • A condition tested before entering a wait() may not be true after the wait is satisfied. Middle 63 Taming Java> Middle 64 Beware of symmetric multi-processing (SMP) environments Visibility • The CPU does not access memory directly. wait is not atomic (5) Taming Java Threads. • There is no way to distinguish a timeout from a notify().holub. (c) 2002 Allen I Holub <www. • CPU read/write requests are given to a “memory unit. (c) 2002 Allen I Holub <www.” which actually controls the movement (at the hardware level) of data between the CPU and main memory> Taming Java Threads. (c) 2002 Allen I Holub <www.holub. (c) 2002 Allen I Holub <> Summarizing wait() behavior • wait() doesn’t return until the notifying thread gives up the> Taming Java Threads.holub. • The order in which changes become visible are not always the order in which the changes are made.holub. (c) 2002 Allen I Holub <www.

) Taming Java Threads.n] MU1 memory MU1 memory • This change is good—it speeds memory access.. (c) 2002 Allen I Holub <www. CPU1 CPU1 memory Memory barriers are created indirectly by synchronization Middle Taming Java Threads. (Loop.holub. for( int i = 0. (c) 2002 Allen I Holub <www.03/06/2003 Some common memory operations are inefficient Presto Chango! • The memory unit notices the inefficiency and rearranges the requests! • Processors supporting a “relaxed memory model” can transfer blocks of memory between cache and the main memory store in undefined order! CPU1 • Consider: W b[n] W b[1] W b[0]R a[n] R a[1] Ra[0] memory int a[] = new int[10]. boolean ) to guard other> Middle 72 false.g. wont_work() Might be –1. • The order in which changes are made may not be the order in which those changes are reflected in main memory. } } public /*not synchronized*/ void { okay = false. } } memory 71 CPU2 ww r r w r MU1 • You cannot use volatile fields (e.holub.holub. testing value. public /*not synchronized*/ void { if( okay ) { do something( field ).holub. (c) 2002 Allen I Holub <www.holub. • To produce: CPU1 CPU1 MU1 Wb[n] Ra[n] Wb[1] Ra[1] Wb[0] Ra[0] Wb[0. Middle 69 Middle Taming Java Threads. Write a zero value to release the mutex w rww rw w r ww r w Avoiding synchronization (revisited) • synchronized is implemented using a memory barrier – so modifications made within a synchronized block will not move outside that block.holub. okay = true. Middle 67 Middle Taming Java Threads.n] Ra[> 68 Don’t Panic BUT… • Reordering doesn’t matter in single-threaded systems. (c) 2002 Allen I Holub <www. (c) 2002 Allen I Holub <www. (c) 2002 Allen I Holub <> Taming Java> 12 . .com> MU2 Taming Java Threads. • The order in which changes are made in the source code may not be preserved at run time! • Reordering not permitted across “memory barriers” (effectively inserted around synchronized access).. (c) 2002 Allen I Holub <www. ++i ) b[i] = a[i]. set if nonzero. enable() Taming Java Threads. -1. int b[] = new int[10].com> class I_wont_work { private volatile boolean okay = private long field = //. field => MU1 CPU2 w w t&s t&s MU2 70 Atomic test/set to acquire mutex. . i < a.

03/06/2003 Even worse Synchronization can fix things • Memory modifications made in the constructor may not be visible.println(s. (c) 2002 Allen I Holub <www. } Holds even if field is final! Thread 1: Surprise s = new Surprise(). private void Singleton factory() { return new Singleton(). } } } return instance. } } } return> Taming Java Threads. Thread 1: synchronized( lock ) { Surprised s = new Surprised().out. } // Synchronizing the following subroutine does // not affect the incorrect behavior. Thread 2: synchronized( lock ) { System.class). public Surprise() { field = -1.holub. (c) 2002 Allen I Holub <> Middle 76 This doesn’t work This doesn’t work either class Still_broken_singleton { public static Singleton instance() { if( instance == null ) { synchronized( Singleton.class ) { if( instance == null ) instance = new Singleton(). instance => Taming Java Threads.class ) { if( instance == null ) { instance = factory(). Middle 73 Taming Java Threads. public final class Singleton { static{ new JDK_11_unloading_bug_fix(Std. private static Singleton instance. (c) 2002 Allen I Holub <www. } } Middle 75 Taming Java Threads. } Thread 2:> Double-checked locking doesn’t work! • Is unreliable even in> Middle 78 Taming Java> Middle 74 The Memory Unit doesn’t know the word “subroutine.field). (c) 2002 Allen I Holub <www. } } return instance. private Singleton(){} // prevent creation by new public static Singleton instance() { if( instance == null ) { synchronized(> 13 . whether or not they are called from a subroutine. Modification of s might become visible before modification of field if memory unit rearranges operations. //. even though the object is accessible! class Surprise { public long field. (c) 2002 Allen I Holub <www. } } • This works Object lock = new Object(). (c) 2002 Allen I Holub <www.class ) { if( instance == null ) { Singleton tmp = new Singleton().holub.CPU machine.get_field()). (c) 2002 Allen I Holub <www.holub. .out. } } Middle 77 Taming Java Threads. .” Taming Java Threads.holub.println(s. } • All code between read/write requests are subject to reordering. } } class Broken_singleton { public static Singleton instance() { if( instance == null ) { synchronized( Singleton.

holub. including the one that caused the class-file load.holub.javaworld. (c) 2002 Allen I Holub <www.) • Memory initialized in a static initializer is safely accessible by all threads. Middle 79 Taming Java Threads.2001/ • Allen Holub: www.cs. all waiting for each other. of Maryland) mailing list: www. (c) 2002 Allen I Holub <www. • ??? Modifications to memory made after a thread is created.html • Brian Goetz: may not be visible to the new> 14 .javaworld. (c) 2002 Allen I Holub <> Taming Java Threads. but before it is> Deadlock: The simplest scenario (1) A few articles on SMP Problems • Paul Jakubik (ObjectSpace): • Two or more threads. www.2001/ jw-0209-toolbox. [must call join()] • Operations are not necessarily executed in source-code order (not relevant if code is synchronized. (c) 2002 Allen I Holub <www.03/06/2003 “Rules to live by” in an SMP environment (gotchas) “Rules to live by” in an SMP environment (things that work) • To assure that shared memory is visible to two threads: the writing thread must give up a lock that is subsequently acquired by the reading MultiprocessorSafe. (c) 2002 Allen I Holub <> Taming Java> Middle 82 Deadlock: The simplest scenario (2) Middle 83 Taming Java Threads.umd.holub. but in different order.pdf • Threads trying to acquire multiple locks. • Bill Pugh (> Deadlock: The simplest scenario (3) Middle 84 Taming Java Threads.html Middle 81 Taming Java Threads. • Modifications made by a thread that terminates are visible to a thread that joins the terminated thread. (c) 2002 Allen I Holub <www. • Modifications made by a thread before it issues a notify() will be visible to the thread that’s released from the associated wait().com> Middle 80 Taming Java j w-02. • Modifications made while sleeping may not be visible after sleep() returns. (c) 2002 Allen I Holub <www.primenet.

synchronized void sock_bam() { batman.} } • The only way to safely terminate a thread is for run() to return normally. has the> • Can happen any time you call a method that can block from any synchronized method. • Consider the following (I've removed exception handling): class Black_hole { private InputStream input = new Socket("www.holub.. } } } }catch(InterruptedException e) }. batman (on thread 2)> Why was stop() deprecated? • The notifying queue blocks if you try to dequeue from an empty queue • NT leaves DLLs (including some system DLLs) in an unstable state when threads are stopped externally. Joker now {/*. (c) 2002 Allen I Holub <www. public synchronized int read() { return> 15 .interrupt() .com> Middle 90 Taming Java Threads. synchronized void to_the_bat_cave() 2.} lock. } public stop() } {t..to_the_bat_cave(). (c) 2002 Allen I Holub <www. { robin = kid. tries to } call lets_go(). { //. (c) 2002 Allen I Holub <> Middle 88 Taming Java Threads. Sidekick robin = new Sidekick(batman).com> interrupt(). stop request*/} public stop() } { t.holub.80) .getInputStream().“Ouch!").. }.com> Taming Java Threads. } public synchronized void close() { input.sock_bam(). } } How do you close the socket? Middle 86 Nested-monitor lockout: another example Taming Java Threads.enqueue(thing). and – other threads can access the partially modified (now unlocked) object public synchronized Object get( ) { return queue.*/} has the lock on robin.close().dequeue().holub. robin. Joker is blocked..lets_go().Thread 2 (Joker) calls synchronized void report(String s) robin. } } Middle 87 Taming Java Threads. blocking_call().Robin tries to report() to { private Boss batman. Middle 89 Taming Java Threads. class Black_hole2 { Notifying_queue queue = new Notifying_queue(). batman. } – but thread may be stopped half way through modifying an object. synchronized Alfred now has the lock on void set_side_kick(Sidekick kid) batman. Middle 85 Taming Java Threads.holub. }. } 3. but can't } because Joker Boss batman = new Boss().set_side_kick( robin ).holub. 5. public synchronized void put(Object thing) { queue. (c) 2002 Allen I Holub <www. } class Sidekick 4. • But there’s no safe way to stop a thread that doesn’t check the “interrupted” flag.holub. blocking_call(). (c) 2002 Allen I Holub <www. but Sidekick(Boss boss){batman = boss.Thread 1 (Alfred) calls { private Sidekick robin.stop(). (c) 2002 Allen I Holub <www.03/06/2003 Nested-monitor lockout Deadlock: A more-realistic scenario class Boss {/*ignore.Thread 1 wakes up. • Code written to depend on an external stop() will have to be rewritten to use interrupted()or isInterrupted(). don’t stop() Why was stop() deprecated (2)? class Right class Wrong { private Thread t = { private Thread t = new Thread() new Thread() { public void run() { public void run() { try { while( true ) { while( !isInterrupted () ) { //.com". (c) 2002 Allen I Holub <www. • stop() causes all monitors held by that thread to be released..} can't because Alfred has the synchronized void lets_go(){.Thread 1 is preempted just { preempt before calling lets_go().

holub. – A thread activates to perform an arbitrary task.holub. } } – wait(). not to provide an exhaustive toolkit. (c) 2002 Allen I Holub <www. – Would leave the stream in an undefined state. (c) 2002 Allen I Holub <www. etc). – Calling interrupted() clears the flag. (Someone has gone into the bathroom. – Only one thread can own at one time. } } The lock is released by wait() before the thread is suspended. Middle 93 Taming Java Threads.holub. only thread per> 16 . – Classic Heisenberg uncertainty: observing the process impacts the process. – Use the classes in java. – My intent is to give you a feel for multithreaded> Taming Java Threads. – Bugs are usually timing related. – I’ll look at a few of the simpler classes covered in depth in Taming Java Threads. } public synchronized void wake_up() { resume().holub.holub. Active Objects). Middle 91 Taming Java Threads. } catch(InterruptedException e) {/*do something reasonable*/} } public synchronized void wake_up() { notify().g. • Low -level solutions (roll-your-own semaphores) • Formal code inspection or pair programming is essential. – Wait while condition false. • Condition Variable • Thread Pools – A group of dormant threads wait for something to do. nio whenever possible.03/06/2003 Why were suspend() and resume() deprecated? interrupt() gotchas • The suspend() method does not release the lock • interrupt() works well only with the methods of the Thread and Object classes class Wrong { public synchronized void take_a_nap() { suspend(). (c) 2002 Allen I Holub <www. all other threads will block on a call to wake_up().com> Given that the best solution isn’t finding a new profession… The big-picture coding issues • Design-to-coding ratio is 10:1 in threaded> Middle 92 class Right { public synchronized void take_a_nap() { try { wait(). – Calling isInterrupted() doesn't clear the flag! Once a thread has entered take_a_nap (). – Blocks if resource is unavailable. • Timers • Counting Semaphore – Allow operation to be performed at regular intervals – Control pool of resources. Middle 95 Taming Java Threads. Middle 96 Taming Java Threads. (c) 2002 Allen I Holub <www. and fallen into a drug -induced coma) • It is not possible to interrupt out of a blocking I/O operation like read().com> Middle 94 Roll your own (A Catalog) Taming Java Threads. – Roll-your-own version can have state. – Instrumented JVMs cannot find all the problems because they change timing. (c) 2002 Allen I Holub <www.> • Block until a predetermined time interval has elapsed • Block until a predetermined time arrives. (c) 2002 Allen I Holub <www. – Roll-your-own version can contain a timeout. (c) 2002 Allen I Holub <www. • Complexity can be reduced with architectural solutions (e. • It's not possible to fully debug multithreaded code in a visual debugger. – It throws an InterruptedException • Everywhere else interrupt() just sets a> Roll your own (2) • Message Queues (interthread communication) • Exclusion Semaphore (mutex) – Thread blocks (with wait/notify) until a message is enqueued. locked the door. join(). sleep(). – You have to test the flag manually all over the place. – Typically. • Debugging multithreaded code takes longer. • Architectural solutions (active objects.holub. Taming Java Threads.

holub. // while initializer runs . } }.length]. } Middle 99 Taming Java Threads.getImage(some_URL).this.holub.holub. class Receiver { //.com> • Synchronous messages—handler doesn't return until it's done doing whatever sender requests • Asynchronous messages —handler returns immediately. Flush_example.out = out. . Flush_example. copy.error(e). } private final OutputStream> 17 .com> Taming Java Threads. (c) 2002 Allen I Holub <www. Middle 98 The Java™-language threading model is not OO Taming Java Threads. Toolkit.03/06/2003 Roll your own (3) Threads from an OO perspective • Reader/Writer Locks • Think messages. } } }. System.write( copy.arraycopy(Flush_example. (c) 2002 Allen I Holub <www.holub. not functions – Allow thread-safe access to global resources such as files: • Must acquire the lock to access a resource • Writing threads are blocked while a read or write operation is in progress • Reading threads are blocked only while a write operation is in progress. (c) 2002 Allen I Holub <www. 0. 0.length = 0. private int length.write_accomplished().com> Middle 100 A more realistic one-thread-permethod example A more realistic one-thread-permethod example // This class demonstrates an asynchronous flush of a // buffer to an arbitrary output stream synchronized void flush( final Error_handler handler ) { new Thread() // Outer object is locked { byte[] copy. } public void run() // Lock is released { try // when run executes { lock. } catch( IOException e ){ handler.length ). Meanwhile request is processed in the background.this ) { // Make local copies of // outer-class fields here. (c) 2002 Allen I Holub <www.length]).buffer. { copy = new byte[> Taming Java Threads. private byte[] buffer. when in> Middle }102 Taming Java Threads.holub. the only methods that run on a thread are methods that are called either directly or indirectly by run(). } class Flush_example { public interface Error_handler { void error( IOException e ).getDefaultToolkit().com> Implementing asynchronous methods —one thread per method • No language-level support for asynchronous messaging. } finally{ lock.holub.this. out. } // Code here doesn't access outer // class (or uses only constants). private final Reader_writer lock = new Reader_writer(). } Middle 101 Taming Java Threads.start(). public Flush_example( OutputStream out ) { this.start(). • Deriving from Thread is misleading – Novice programmers think that all methods of a class that extends Thread run on that thread. Simultaneous reads are okay Middle 97 Taming Java Threads. } • Threading system is based entirely on procedural notions of control flow. (c) 2002 Allen I Holub <www.this.holub.this. public asynch_method() { new Thread() { public void run() { synchronized( Receiver. 0. (c) 2002 Allen I Holub <www. . copy. (c) 2002 Allen I Holub <www.request_write().

.com> Middle 106 Taming Java Threads. } } Middle 105 Taming Java Threads. } } public synchronized void execute(Runnable action) { pool.Closed e){/* ignore */} catch(Throwable e) { // handle unexpected error gracefully.holub.holub.8021 ms. Tibco …) – Ada and Intel RMX (circa 1979) Middle 107 Taming Java Threads. (c) 2002 Allen I Holub <> Middle 104 Implementing a simple thread pool Taming Java Threads.printStackTrace().enqueue( action )> Implementing a simple thread pool private final class Pooled_thread extends Thread { public void run() { synchronized( startup_lock ) {} try { while( !isInterrupted() ) ((Runnable )pool. (c) 2002 Allen I Holub <> Middle 108 Taming Java Threads. • Asynchronous requests are executed serially on a thread created for that purpose. • Think Tasks – An I/O task. (c) 2002 Allen I Holub <www.holub. (c) 2002 Allen I Holub <www.holub.close().start().com> 18 . e. (c) 2002 Allen I Holub <www. = . all but one of the threads are typically blocked.03/06/2003 Problems with one-thread-permethod strategy Use Thread Pools • The real version: • It is a worse-case synchronization scenario. (c) 2002 Allen I Holub <> A generalized active object The Active Object design pattern • An architectural solution to threading synchronization. } } } public Simplified_Thread_pool(int pool_size ) { synchronized( startup_lock ) { while( --pool_size >= 0 ) new Pooled_thread(). • The solution can be generalized in the Java programming language like this: dequeue() blocks (using wait/notify) until there’s something to get. } catch(InterruptedException e){/* ignore */} catch(Blocking_queue. – Shrinks back down to original size when extra threads aren’t needed – Supports a “lazy” close.0491 ms. private final Blocking_queue pool = new Blocking_queue(). – Many threads all access the same outer-class object simultaneously – Since synchronization is required. – Grows from the initial size to a specified maximum if necessary.0040 ms. (c) 2002 Allen I Holub <www. • Thread-creation overhead can be stiff: Create String Create Thread Create & start Thread Middle 103 = . (NT 4. = . waiting to access the object. for example. public final class Simplified_Thread_pool { private Object startup_lock = new Object().. – Message-oriented Middleware (> Taming Java Threads. accepts asynchronous read requests to a single file and executes them serially.holub. 600MHz) Taming Java Threads.dequeue()).run(). } public synchronized void close() { pool.holub.

100).dispatch ( new Runnable () { public void run() { System. (c) 2002 Allen I Holub <www.holub.swing.println(s).apress. like it or not – GUI code is multithreaded – No telling where your code will be used in the future public static void println(final String s) { dispatcher. etc.holub.holub.holub. while((to_do=(Runnable )( requests. Allen I. Middle End 113 Taming Java Threads. (c) 2002 Allen I Holub <www. yield().com> Middle End 112 Taming Java Threads. } } ).com> In-depth coverage and code For in-depth coverage.holub. } } )> Using an active object (detangling UNIX™ console output) Summing up class Console { private static Active_object dispatcher = new Active_object(). These notes may not be redistributed. these slides. } • Programming threads is neither easy nor> Taming Java Threads. } public void run() { try { Runnable to_do. • Use good architecture.* thread is an active object Implementing Active Object • Swing/AWT uses it's own thread to handle the incoming OS-level messages and to dispatch appropriate notifications to> 19 .out. (c) 2002 Allen I Holub <www. • The threading system isn’t object oriented. public Active_object(){ setDaemon( true ). • synchronized is your friend. • Swing is not thread safe. } Middle 111 Taming Java Threads.holub. not semaphores. • Supplement language-level primitives to do real For source code. Middle 109 Taming Java These notes © 2003. } }catch( InterruptedException e ){} } public final void dispatch(Runnable operation ) { requests. static{ dispatcher.03/06/2003 The javax. All rights reserved. • You have to worry about threads.dequeue()))!= null) { to_do.enqueue( operation ). go to my web page www. Holub. } 110 } Taming Java Threads. see Taming Java™ Threads (www. } private Console(){} • Java™-language threads are not platform independent—they can't be. (c) 2002 Allen I Holub < (c) 2002 Allen I Holub <www. (c) 2002 Allen I Holub <> Middle public class Active_object extends Thread { private Notifying_queue requests = new Notifying_queue(). except that you may print them for your personal use.start(). These notes may not be reproduced by any means without the writt en permission of the author. • The Swing subsystem is effectively a “UI task” to which you enqueue requests: SwingUtilities. setSize(200. to_do = null..invokeLater // enqueue a request ( new Runnable() { public void run() { some_window. Grit your teeth and use it.