...
Unfortunately, the stop()
method incorrectly uses the Thread.getState()
method to check whether the thread is blocked and has not terminated before delivering the notification. Using the Thread.getState()
method for synchronization control, such as checking whether a thread is blocked on a wait, is inappropriate. Java Virtual Machines (JVMs) are permitted to implement blocking using spin-waiting; consequently, a blocked thread may never enter the WAITING
or TIMED_WAITING
state [Goetz 2006]. Because the thread may never enter the WAITING
state, the stop()
method might fail to terminate the thread.
If doSomething()
and stop()
are called from different threads. It is possible for stop()
to fail to see the initialized thread
even if doSomething()
was called earlier, unless there is a happens-before relationship between the two calls. If the two methods are invoked by the same thread, they automatically have a happens-before relationship, so they don't have this problem.
Compliant Solution
This compliant solution removes the check for determining whether the thread is in the WAITING
state. This check is unnecessary because invoking notifyAll()
affects only threads that are blocked on a wait()
invocation.
...