Thread Constellations with Examples in Programming Language | CMSC 433, Study notes of Programming Languages

Material Type: Notes; Professor: Hicks; Class: PROG LANG TECH & PDGMS; Subject: Computer Science; University: University of Maryland; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-3uq
koofers-user-3uq 🇺🇸

10 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSC 433, Fall 2002
-
Michael Hicks
1
CMSC 433, Fall 2002
-
Michael Hicks
64
The Final Answer (I hope!)
public class
ProducerConsumer
{
private boolean
valueReady
= false;
private Object value;
private Object
fullLock
=new Object(),
emptyLock
=new Object();
void produce(Object o) {
// no longer synchronized method
synchronized (
emptyLock
) {
while (
valueReady
) {
emptyLock
.wait();
}
}
synchronized (this) { value = o;
valueReady
= true; }
synchronized (
fullLock
) {
fullLock
.notify(); }
}
CMSC 433, Fall 2002
-
Michael Hicks
65
The Final Answer (I hope!)
Object consume() { // no longer synchronized method
synchronized (
fullLock
) {
while (!
valueReady
) {
fullLock.wait();
}
}
Object o;
synchronized (this) {
valueReady
= false;
o = value;
value = null;
}
synchronized (
emptyLock
) {
emptyLock
.notify(); }
return o;
}
}
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Thread Constellations with Examples in Programming Language | CMSC 433 and more Study notes Programming Languages in PDF only on Docsity!

CMSC 433, Fall 2002 - Michael Hicks 64

The Final Answer (I hope!)

public class ProducerConsumer { private boolean valueReady = false; private Object value; private Object fullLock=new Object(), emptyLock=new Object();

void produce(Object o) { // no longer synchronized method synchronized (emptyLock) { while (valueReady) { emptyLock.wait(); } } synchronized (this) { value = o; valueReady = true; } synchronized (fullLock) { fullLock.notify(); } }

CMSC 433, Fall 2002 - Michael Hicks 65

The Final Answer (I hope!)

Object consume() { // no longer synchronized method synchronized (fullLock) { while (!valueReady) { fullLock.wait(); } } Object o; synchronized (this) { valueReady = false; o = value; value = null; } synchronized (emptyLock) { emptyLock.notify(); } return o; } }

CMSC 433, Fall 2002 - Michael Hicks 66

notify() vs. notifyAll()

• Very tricky to use notify() correctly

– notifyAll() generally much safer

• To use notify() correctly, should:

– have all waiters be equal

• each notify only needs to wake up 1 thread

• doesn’t matter which thread it is

– handle exceptions correctly

CMSC 433, Fall 2002 - Michael Hicks 67

Thread Cancellation

• Example scenarios: want to cancel thread

– whose processing the user no longer needs (i.e.

she has hit the “cancel” button)

– that computes a partial result and other threads

have encountered errors, … etc.

• Java used to have support Thread.kill()

– But it and Thread.stop() are deprecated

– Use Thread.interrupt() instead

CMSC 433, Fall 2002 - Michael Hicks 70

InterruptedException

• Will be thrown if interrupted either while

doing or attempting to do a wait, sleep, or

join

– But not when blocked (or blocking on) on a

lock or I/O

• Must reset invariants before cancelling

– E.g. closing file descriptors, notifying other

waiters, etc.

CMSC 433, Fall 2002 - Michael Hicks 71

InterruptedException Example

• Threads t1 and t2 are waiting

• Thread t3 performs a notify

– thread t1 is selected

• Before t1 can acquire lock, t1 is interrupted

• t1’s call to wait throws InterruptedException

– t1 doesn’t process notification

– t2 doesn’t wake up

CMSC 433, Fall 2002 - Michael Hicks 72

Handling InterruptedException

synchronized (this) {

while (!ready) {

try { wait(); }

catch (InterruptedException e) {

// make shared state acceptable

notify();

// cancel processing

return;

// do whatever

CMSC 433, Fall 2002 - Michael Hicks 73

Deadlock

• Quite possible to create code that deadlocks

– Thread 1 holds lock on A

– Thread 2 holds lock on B

– Thread 1 is trying to acquire a lock on B

– Thread 2 is trying to acquire a lock on A

– Deadlock!

• Not easy to detect when deadlock has

occurred

– other than by the fact that nothing is happening

CMSC 433, Fall 2002 - Michael Hicks 76

Livelock

• Deadlock arises when blocked threads

cannot execute

• Livelock occurs when threads actually are

executing, but no work gets done.

– E.g. one thread keeps performing work that gets

undone by another thread, so it must keep

starting over

CMSC 433, Fall 2002 - Michael Hicks 77

Field Visibility

• Threads might cache values

• Obtaining a lock forces the thread to get fresh

values

• Releasing a lock forces the thread to flush out all

pending writes

• volatile variables are never cached

• sleep(…) doesn’t force fresh values

• Many compilers don’t perform these optimizations

– but some do (Hotspot?)

• Problem might also occur with multiple CPUs

CMSC 433, Fall 2002 - Michael Hicks 78

Guidelines to simple/safe multi-

threaded programming

• Synchronize access to shared data

• Don’t hold a lock on more than one object at a

time

– could cause deadlock

• Hold a lock for as little time as possible

– reduces blocking waiting for locks

• While holding a lock, don’t call a method you

don’t understand

– e.g., a method provided by someone else, especially if

you can’t be sure what it locks

CMSC 433, Fall 2002 - Michael Hicks 79

Guidelines (cont.)

• Have to go beyond these guidelines for

more complex situations

– but need to understand threading and

synchronization well

• We’ll discuss threads more from the

textbook Concurrent Programming in Java

and from a talk at a Java conference by Bill

Pugh and Doug Lea