I’m reading through my SCJP book and I’m on the chapter about threads, and I am wondering why you should use notify at all.
Here is my sample program using notify:
class ThreadA {
public static void main(String[] args){
ThreadB b = new ThreadB();
b.start();
synchronized(b){
try{
System.out.println("Waiting for b to complete...");
b.wait();
}catch(InterruptedException e) {}
System.out.println("Total is: " + b.total);
}
}
}
class ThreadB extends Thread {
int total;
public void run() {
synchronized(this) {
for(int i = 0; i < 100000000; i++){
total++;
}
notify();
}
for(int i = 0; i < 100000000; i++){
total++;
}
}
}
If I take out the notify call, it still executes in the same way. I.E., once the lock is released, the b.wait() stops blocking eventually and we get a semi random number between 100000000 and 200000000, depending on the scheduler.
This code:
class ThreadA {
public static void main(String[] args){
ThreadB b = new ThreadB();
b.start();
synchronized(b){
try{
System.out.println("Waiting for b to complete...");
b.wait();
}catch(InterruptedException e) {}
System.out.println("Total is: " + b.total);
}
}
}
class ThreadB extends Thread {
int total;
public void run() {
synchronized(this) {
for(int i = 0; i < 100000000; i++){
total++;
}
notify();
}
}
}
Always results in 100000000 getting printed regardless wither the notify is there or not.
And this code:
class ThreadA {
public static void main(String[] args){
ThreadB b = new ThreadB();
b.start();
synchronized(b){
try{
System.out.println("Waiting for b to complete...");
b.wait();
}catch(InterruptedException e) {}
System.out.println("Total is: " + b.total);
}
}
}
class ThreadB extends Thread {
int total;
public void run() {
synchronized(this) {
for(int i = 0; i < 100000000; i++){
total++;
}
notify();
for(int i = 0; i < 100000000; i++){
total++;
}
}
}
}
Always prints out 200000000 regardless of the notify being present or absent.
So as far as I can tell, the only thing the notify does is possibly waking up a thread earlier than needed, if that is the case, why use notify at all? Why not wait for the lock to be released and let the JVM restart the other thread?
Unless you or someone else calls
notify(), thewait()should continue forever, barring spurious wakes. Are you sure nothing’s interrupting the thread?Basically you use
notify()precisely to wake up threads. If you don’t need to put a thread to sleep until another thread can notify it that it should wake up, don’t use it.EDIT: Okay, I’ve reproduced this behaviour – and I suspect it’s because you’re calling
wait()on the thread object itself. My guess is that theThreadobject gets notified when the thread terminates.Try waiting on a shared plain
Object, like this:If you uncomment that line, the program terminates – otherwise it doesn’t.
EDIT: I’ve actually found some documentation on this. From
Thread.join(millis, nanos):