what exactly does "wait()" and "notify()" methods do?
When any thread invoke wait() method thread goes from "runnig state" to "waiting state".
And when target thread invokes "notify()" that thread which was in the waiting state goes to runnable state.
When any thread invoke wait() method thread goes from "runnig state" to "waiting state".
And when target thread invokes "notify()" that thread which was in the waiting state goes to runnable state.
Chan Ag wrote:Which parts you could not understand?
When any thread invoke wait() method thread goes from "runnig state" to "waiting state".
And when target thread invokes "notify()" that thread which was in the waiting state goes to runnable state.
The first line is correct.
The next line is not right, for a thread that issued wait() still needs to acquire the synchronization lock to return from the wait() method. The notify() just sends a notification to one of the waiting threads.. The choice of which thread would get the notification is implementation specific.
Also a notify() only sends the notification to a waiting thread. Even after notify() is issued, the notifying thread might still be holding the synchronization lock.
The choice of which thread would get the notification is implementation specific.
salih ayan wrote:
my question is;
when "notify()" method invoked by target thread, and suppose that there is only two thread and another thread was in "waiting state" has got notification from target thread,
(my question is actually about after this point ) the thread which was in "waiting state" (as you told me) must migrate "runnable state". (am l right?)
Sharmistha Sarkar wrote:Hello All, this post is very similar to my problem, so I didn't create another post . I am writing a code to check how wait () and notify() works and always I am seeing; after notify() gets call, the previous waiting thread never executes . I want output like the below lines.
started.......
It is notify block .......
Thread woke up now .......
End of code .......
Could anyone please look into my code and help me to make the program ?
Sharmistha Sarkar wrote:Hi Henry, I am getting below output of my program .
started.......
It is notify block .......
Sharmistha Sarkar wrote:
But if I give wait(specify some time) then the program runs as expected.
I mean to say, I gave wait(100) instead wait() in the program and Output shows,
started.......
It is notify block .......
Thread woke up now .......
End of code .......
This is what I want. After notify() calls it goes back to the wait() related synchronized block again and executes the remaining of that block and then comes out and prints the last statement.
Could you please explain what is happening here?
As already mentioned, if the notification is sent before the other thread waits, the other thread will wait forever, as the notifications without any threads waiting is discarded.
Sharmistha Sarkar wrote:
Now, is there any tricks is there to complete the waiting of a thread and then sends notification....I am little confused here. let me give you another example; I found in some site , where wait() and notify() works properly. So what is the difference between my code and the below code?
Did you just should on me? You should read this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|