"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().