The methods wait(...)
, notify()
and notifyAll()
are available on a Thread
instance, but only
because all classes in Java extend Object
and therefore automatically inherit those methods. But there are two very good reasons for not
calling them on a Thread
:
- Internally, the JVM relies on these methods to change the state of the Thread (
BLOCKED
, WAITING
, …), so calling them
will corrupt the behavior of the JVM.
- It is not clear (perhaps even to the original coder) what is really expected. For instance, it is waiting for the execution of the Thread to
suspended, or is it the acquisition of the object monitor that is waited for?
Noncompliant Code Example
Thread myThread = new Thread(new RunnableJob());
...
myThread.wait(2000);