I was reading about inter thread communication in Java using wait/notify/notifyAll.
I went thru this answer: https://stackoverflow.com/a/36276832
There are 2 threads and 1 main thread. Main thread do notifyAll
. It wakes up remaining 2 threads, and both the threads prints
":syncronized block have finished"
But i have read that if 2 threads are waiting for a lock, notifyAll will wake up each thread but lock will be acquired by only 1 thread.
So my question is how come both t1 and t2 thread are completing their execution?
When I change from lock.notifyAll();
to lock.notify();
, the Java program never ends.
One thread either of t1/t2 will be in waiting state.
Could somebody please answer. I can explain further my doubt in case not understood clearly.
Problem in easy words: If 2 threads are waiting for same lock and 3rd thread do notifyAll, only one of them gets the lock, the other one remain in waiting state, so in the above case, how come both threads are able to complete execution?