project
play

Project Minimum submission Threads Deadline extended to tonight - PDF document

Project Minimum submission Threads Deadline extended to tonight at midnight Early submitters 10 point bonus Synchronization TSP Still due on Tuesday! Before we begin Plan for today Thread synchronization


  1. Project • Minimum submission Threads – Deadline extended to tonight at midnight – Early submitters – 10 point bonus Synchronization • TSP – Still due on Tuesday! Before we begin Plan for today • Thread synchronization • Any questions? • Questions with threads? Thread Thread • A thread is a single sequential flow of • A thread is not a program control within a program – It cannot run on its own – Runs within a program • Sometimes called: – Execution context – Lightweight process

  2. Thread Thread States The start() method places a • Multiple threads can be running at the same thread in the ready state Ready The scheduler selects a thread time within a single program 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() • join () is NOT a static method public class WorkerThread extends Thread { private int result = 0; • Calling join() on a thread will make the public void run() { // Perform a complicated time consuming calculation caller wait until the thread is done // 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 ); } } Synchronization: Motivation Synchronization: Motivation public class ConcAcess extends Thread { • One possible solution private static int common = 0; public void run() { int local = 0; local = common; local = local + 1; common = local; } public static void main( String args[] ) { Thread myThread = new Thread[3]; for ( int i = 0; i < 3; i++ ) { myThread[ i ] = new ConcAccess(); myThread[ i ].start() } }

  3. Synchronization: Motivation Critical section • Another possible scenerio • 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 Synchronization: Motivation public class ConcAcess extends Thread { • Every Java Object contains a lock that can private static int common = 0; Obtain lock here be held by at most one thread at any given public void run() { int local = 0; time Hold lock local = common; local = local + 1; Release lock • Allows for mutually exclusive access to a common = local; } sequence of code public static void main( String args[] ) { Thread myThread = new Thread[3]; for ( int i = 0; i < 3; i++ ) { myThread[ i ] = new ConcAccess(); myThread[ i ].start() } } synchronized Example with Java locks public class ConcAccess extends Thread { • A thread can obtain the lock on a Java private static int common = 0; private static final Integer lock = new Integer (0); object by using the synchronized public void run() { int local = 0; statement. synchronized (lock) { local = common; local = local + 1; common = local; } } public static void main( String args[] ) { Thread myThread = new Thread[3]; for ( int i = 0; i < 3; i++ ) { myThread[ i ] = new ConcAccess(); myThread[ i ].start() } }

  4. Bad Example with Java locks Threads and Locks public class ConcAcess extends Thread { private static int common = 0; private Integer lock; public ConcAcess () { lock = new Integer(0); } public void run() { int local = 0; synchronized (lock) { local = common; local = local + 1; common = local; } } public static void main( String args[] ) { Thread myThread = new Thread[3]; • Questions? for ( int i = 0; i < 3; i++ ) { myThread[ i ] = new ConcAccess(); myThread[ i ].start() } } Java locks with this Synchronized methods • An object can choose to grab a lock to itself • Declaring a method to be synchronized is a shorthand for grabbing the lock on this . when running one of its methods public someMethod() { synchronized( this ) public synchronized someMethod() { … } { … } • Is the same as } public someMethod() { synchronized( this ) { … } } Synchronization and Java Synchronized Static Methods Collections • From HashSet javadoc: • Java also provides synchronized static methods. • Before a synchronized static method is executed, – Note that this implementation is not the calling thread must first obtain the class lock. synchronized. • Since there is only one class lock, at most one • If multiple threads access a set concurrently, and at least one of the threads modifies the set, it must be thread can hold the lock for the class (object locks synchronized externally. can be held by different threads locking on – Meaning different instances of the class). • Different threads can manipulate the collection concurrently.

  5. Wrapper Implementations synchronization wrappers • Wrapper implementations add some • The synchronization wrappers, will make the Collection thread safe. functionality on top of what a collection • It is imperative that the user manually synchronize offer on the returned set when iterating over it: – Synchronization Set s = Collections.synchronizedSet(new HashSet()); – Unmodifiable ... synchronized(s) { • Wrappers simply delegate all of their real Iterator i = s.iterator(); // Must be in the block while (i.hasNext()) foo(i.next()); work to a specified collection } synchronization wrappers Synchronization • Questions? 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); • Let’s go through some code.

Recommend


More recommend