Reputation: 75
When I call wait() method on a thread object, the waiting thread will be waken up when the synchrized thread finishes run, why is the thread object behaviors different from plain object where wait()?
Thread thread1 = new Thread(()-> {
System.out.println("thread 1 start");
try {
Thread.sleep(3000);
System.out.println("thread over");
} catch (InterruptedException e) {
e.printStackTrace();
}});
thread1.start();
synchronized (thread1) {
try {
thread1.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("main thread wake up");
I expected main thread does not wake up after 3s, but not.
Upvotes: 0
Views: 42
Reputation: 692121
The javadoc says:
As a thread terminates the
this.notifyAll
method is invoked. It is recommended that applications not use wait, notify, or notifyAll on Thread instances.
Upvotes: 5