140 likes | 237 Vues
CSCI 6900: Design, Implementation, and Verification of Concurrent Software. Eileen Kraemer August 19 th , 2010 The University of Georgia. Concurrency Concepts . See: A tutorial we created . Java Threads & Concurrency. Liveness Deadlock Starvation and Livelock Guarded Blocks
E N D
CSCI 6900: Design, Implementation, and Verification of Concurrent Software Eileen Kraemer August 19th, 2010 The University of Georgia
Concurrency Concepts • See: A tutorial we created ...
Java Threads & Concurrency • Liveness • Deadlock • Starvation and Livelock • Guarded Blocks • Immutable Objects • A Synchronized Class Example • A Strategy for Defining Immutable Objects • High Level Concurrency Objects • Lock Objects • Executors • Executor Interfaces • Thread Pools • Concurrent Collections • Atomic Variables
Deadlock • See example program “Deadlock”
Starvation • describes a situation where a thread is unable to gain regular access to shared resources and is unable to make progress. • happens when shared resources are made unavailable for long periods by "greedy" threads. • For example, suppose an object provides a synchronized method that often takes a long time to return. If one thread invokes this method frequently, other threads that also need frequent synchronized access to the same object will often be blocked.
Livelock • A thread often acts in response to the action of another thread. If the other thread's action is also a response to the action of another thread, then livelock may result. • As with deadlock, livelocked threads are unable to make further progress. However, the threads are not blocked — they are simply too busy responding to each other to resume work.
Guarded Blocks • Threads often have to coordinate their actions. • The most common coordination idiom is the guarded block. • block begins by polling a condition that must be true before the block can proceed. public void guardedJoy() { //Simple loop guard. Wastes processor time. Don't do this! while(!joy) { } System.out.println("Joy has been achieved!"); }
More efficient guard • invoke Object.wait to suspend the current thread. • invocation of wait does not return until another thread has issued a notification that some special event may have occurred — though not necessarily the event this thread is waiting for – so wait inside a loop that checks the condition public synchronized guardedJoy() { //This guard only loops once for each special event, which may not //be the event we're waiting for. while(!joy) { try { wait(); } catch (InterruptedException e) {} } System.out.println("Joy and efficiency have been achieved!"); }
wait public synchronized guardedJoy() { //This guard only loops once for each special event, which may not //be the event we're waiting for. while(!joy) { try { // owns the lock on this object wait(); // on call: release lock & suspend // on return: must re-aquire lock // before continuing } catch (InterruptedException e) {} } System.out.println("Joy and efficiency have been achieved!"); }
notifyAll • While our first thread is waiting, some other thread will acquire the lock and invoke Object.notifyAll(), which informs all threads waiting on that lock that something “interesting” has happened public synchronized notifyJoy() { // obtains lock joy = true; notifyAll(); } // releases lock … at some point after this, the waiting thread re-acquires the lock
notify vs. notifyAll • Notify wakes up one waiting thread, rather than all waiting threads
Producer/ Consumer example • See: Drop example
Assignment One • Create a producer/consumer system in which • the producer counts from 1 to 10, and places the current count into a shared object that you define. • The consumer reads the values from the shared object and displays them to the screen.
Immutable Objects • state cannot change after it is constructed. • useful in concurrent applications. Since they cannot change state, they cannot be corrupted by thread interference or observed in an inconsistent state.