Project Threads Synchronization Minimum submission Deadline extended to tonight at midnight Early submitters 10 point bonus TSP Still due on Tuesday! Before we begin Plan for today Thread synchronization Any questions? Questions with threads? Thread A thread is a single sequential flow of control within a program Sometimes called: Execution context Lightweight process Thread A thread is not a program It cannot run on its own Runs within a program
Thread Thread States Multiple threads can be running at the same time within a single program The start() method places a thread in the ready state Ready The scheduler selects a thread and places it in the running state Waiting Running A thread that is waiting for I/O, was suspended, is sleeping, blocked, or otherwise is unable to do any more work is placed in the waiting state join() join () is NOT a static method Calling join() on a thread will make the caller wait until the thread is done join() public class WorkerThread extends Thread { private int result = 0; // Perform a complicated time consuming calculation // and store the answer in the variable result public static void main(string args[]) { WorkerThread t = new WorkerThread(); t.start(); try { t.join(); catch ( InterruptedException ex ) { System.out.println( result ); One possible solution
Another possible scenerio Critical section Critical section block of code that must be executed by at most 1 thread at a time. The updating of common in the last example (run method) would be considered a critical section. Java locks Every Java Object contains a lock that can be held by at most one thread at any given time Allows for mutually exclusive access to a sequence of code Obtain lock here Hold lock Release lock synchronized A thread can obtain the lock on a Java object by using the synchronized statement. Example with Java locks public class ConcAccess extends Thread { private static final Integer lock = new Integer (0); synchronized (lock) {
Bad Example with Java locks Threads and Locks private Integer lock; public ConcAcess () { lock = new Integer(0); synchronized (lock) { Questions? Java locks with this An object can choose to grab a lock to itself when running one of its methods public somemethod() { synchronized( this ) { Synchronized methods Declaring a method to be synchronized is a shorthand for grabbing the lock on this. public synchronized somemethod() { Is the same as public somemethod() { synchronized( this ) { Synchronized Static Methods Java also provides synchronized static methods. Before a synchronized static method is executed, the calling thread must first obtain the class lock. Since there is only one class lock, at most one thread can hold the lock for the class (object locks can be held by different threads locking on different instances of the class). Synchronization and Java Collections From HashSet javadoc: Note that this implementation is not synchronized. If multiple threads access a set concurrently, and at least one of the threads modifies the set, it must be synchronized externally. Meaning Different threads can manipulate the collection concurrently.
Wrapper Implementations Wrapper implementations add some functionality on top of what a collection offer Synchronization Unmodifiable Wrappers simply delegate all of their real work to a specified collection synchronization wrappers The synchronization wrappers, will make the Collection thread safe. It is imperative that the user manually synchronize on the returned set when iterating over it: Set s = Collections.synchronizedSet(new HashSet());... synchronized(s) { Iterator i = s.iterator(); // Must be in the block while (i.hasnext()) foo(i.next()); synchronization wrappers Synchronization public static Collection synchronizedcollection(collection c); public static Set synchronizedset(set s); public static List synchronizedlist(list list); public static Map synchronizedmap(map m); public static SortedSet synchronizedsortedset(sortedset s); public static SortedMap synchronizedsortedmap(sortedmap m); Questions? Let s go through some code.