2

"A thread gets on to waiting list by executing the wait() method of the target object. From that moment, it doesn't execute any further instructions until the notify() method of the target object is called."

How guaranteed the above statement is?
While the below program isn't resulting the same as per the above statement and when I comment notify().

i.e

public class ThreadInterations {
   public static void main(String[] args) {
       TargetThread targetThread = new TargetThread();
       targetThread.setName("Lucy");
       targetThread.start();

       synchronized(targetThread) {
          try {
            System.out.println(Thread.currentThread().getName()+" is going to wait");
            targetThread.wait();
            System.out.println(Thread.currentThread().getName()+ " is done waiting");
           } catch (InterruptedException e) {
             e.printStackTrace();
           }
           System.out.println("The Amount Value is: "+targetThread.amount);
       }
   }
}

And the target Thread is

class TargetThread extends Thread {
   int amount;

   @Override
   public void run() {
      synchronized(this) {
        System.out.println(Thread.currentThread().getName()+" is going to notify");
        for(int i = 0; i < 100; i++) {
            amount += i;
        }
        notify();
        System.out.println(Thread.currentThread().getName()+" is done notifying");
      }
   }
}

Output with notify

main is going to wait
Lucy is going to notify
Lucy is done notifying
main is done waiting
The Amount Value is: 4950

Output without notify

main is going to wait
Lucy is going to notify
Lucy is done notifying
main is done waiting
The Amount Value is: 4950

As per my knowledge, Thread in waiting state shouldn't execute the rest of it's code until notify on the same object is called. Correct me If my understanding on this particular concept was wrong? If it's correct, Why am I getting amount value in second output even though I've commented notify().

Kishore Kumar Korada
  • 1,204
  • 6
  • 22
  • 47
  • 1
    That opening statement is incorrect - [a thread can wake up spuriously (i.e. without being notified)](https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#wait-long-). – assylias Mar 10 '18 at 09:27
  • See duplicate for an explanation - synchronize on an object that is not the thread and that should work better, although as commented above, it is still possible that the thread will wake up spuriously (but not very likely). – assylias Mar 10 '18 at 09:35
  • @assylias It may be a best practice synchronize on an object that isn't a thread work. But Thread is also an object. So what's wrong with it? Like you said, Spurious wake up might make sense but the above program isn't such complex that it would wake up spuriously. Answer specified in duplicate was very narrow and I didn't find much help in it – Kishore Kumar Korada Mar 10 '18 at 09:51
  • The answer in the duplicate is quite clear: when a thread dies it calls notify on itself. So basically it's as if there were a `notify()` at the end of the `run` method. And as discussed in another answer, the javadoc of Thread.join explicitly recommends not to use `wait` or `notify` on a Thread object. Just use a `static final Object lock = new Object();` as a monitor and your code will work fine. – assylias Mar 10 '18 at 09:56
  • Ok. Now it cleared to me. I was not aware of that a dying thread calls notify on itself and thank you for pointing out to use join. I need to do little more work :) – Kishore Kumar Korada Mar 10 '18 at 10:01

0 Answers0