Download presentation
Presentation is loading. Please wait.
Published byBrooke Conley Modified over 9 years ago
1
1 CSCI 6900: Design, Implementation, and Verification of Concurrent Software Eileen Kraemer August 19 th, 2010 The University of Georgia
2
2 Concurrency Concepts See: A tutorial we created...
3
3 Java Threads & Concurrency Liveness Liveness Deadlock Starvation and Livelock Guarded Blocks Guarded Blocks Immutable Objects Immutable Objects A Synchronized Class Example A Strategy for Defining Immutable Objects High Level Concurrency Objects High Level Concurrency Objects Lock Objects Executors Executor Interfaces Executor Interfaces Thread Pools Thread Pools Concurrent Collections Concurrent Collections Atomic Variables Atomic Variables
4
4 Deadlock See example program “Deadlock”
5
5 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.
6
6 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.
7
7 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!"); }
8
8 More efficient guard invoke Object.wait to suspend the current thread.Object.wait 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!"); }
9
9 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!"); }
10
10 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
11
11 notify vs. notifyAll Notify wakes up one waiting thread, rather than all waiting threads
12
12 Producer/ Consumer example See: Drop example
13
13 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.
14
14 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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.