COMP 402: Production Programming← Concurrency Constructs → |
Home — Spring 2012 | | | Information | | | Resources | | | DrJava | | | SourceForge | | | Blog |
volatile long _count
)synchronized(this) { ... }
block that encloses the entire method.synchronized(MyClass.class) { ... }
block that encloses the entire method.synchronized(o) { ... }
before you can call o.wait();
or o.notify();
or o.notifyAll();
o.wait(milliSeconds)
someThread.interrupt();
o.wait()
can return spuriously, i.e. without proper reasono.notify();
is called before another thread has called o.wait();
, the notification will get lost because it was sent too early.o.notifyAll();
, the o.wait();
call should be embedded in a while
loop and check that the desired condition is actually true:hasConditionBeenMet
and before calling lock.wait();
or another thread might set hasConditionBeenMet
to true
after a thread has determined it to be false
but before waiting. This would cause an "early notification" problem.hasConditionBeenMet
not have to be volatile here? Any changes done under the same lock are visible to other threads:Thread 1 | Thread 2 |
---|---|
|
|
o.wait();
releases the lock on o
, and after being woken up by a o.notify();
or o.notifyAll();
, a thread competes for the lock on o
again and automatically re-acquires it before continuing after the o.wait();
.
o.wait();
only releases the lock on o
, not on other locks held. If the thread that waits also holds other locks, and the thread that notifies it needs one of those, there will be a deadlock.wait()
should be in a while loop or not. This depends on the choice of how to wake threads up.URL: http://www.cs.rice.edu/~javaplt/402/12-spring/lectures/conc2/index.shtml