• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

what exactly does "wait()" and "notify()" methods do

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi to all.
Yes my question is below ;

what exactly does "wait()" and "notify()" methods do?



l have an idea please check whether is true ;



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.



thanks for your explanations..
 
Bartender
Posts: 2237
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can read about this here.
Also check this and this.
 
salih ayan
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pawel Pawlowicz wrote:You can read about this here.
Also check this and this.



Hi l read but l couldnt find answer of my question.
Please help..
Thanks...
 
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
salih ayan
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



Hi Chan...
l agree with you said that just below sentence

The choice of which thread would get the notification is implementation specific.



l also before knew and understood that this point.

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?)

Thanks again Chan..
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?)



The wait() method gives up the synchronization lock prior to waiting -- so, upon receiving notification, it must reacquire the lock. It isn't as simple as going from the waiting state to the runnable state. It may be in a blocked state while it is reacquiring the synchronization lock.

Henry
 
Ranch Hand
Posts: 50
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?

 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ?



When a notification is sent, and there are no threads waiting for the notification, then the notification is simply discarded. In your program, this will happen if the thread that does the notification sends it before the other thread calls wait.

Henry
 
Sharmistha Sarkar
Ranch Hand
Posts: 50
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry, I am getting below output of my program .

started.......
It is notify block .......

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?

Regards,
Sharmistha
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sharmistha Sarkar wrote:Hi Henry, I am getting below output of my program .

started.......
It is notify block .......



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:
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?



The output is what you want -- however, the behavior may not be what you want.

The wait(100) puts a 100 millisecond timeout on the wait call. This means that the wait method will return after 100 milliseconds, if it doesn't receive a notification. If you remove the notification from your program, you will notice that the wait(100) method also returns.

Henry
 
Sharmistha Sarkar
Ranch Hand
Posts: 50
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes yes , I also noticed without a notify() also it works when I give wait(100).

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.



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?





OUTPUT:

Producer thread running ....
Waiting for return key.

Return key pressed.
Resumed.

Thanks,
Sharmistha
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?



The reason the second code works is because there is a two second delay on the notification thread. With a two second delay on the notification thread, it pretty much guarantees that the wait thread will get to the wait() method first.

BTW, using time delays to coordinate threads is silly. After all, how do you coordinate lots of threads? Put in different delays, with some of them being delayed an hour? If this is the type of example that you are getting from your tutorials, perhaps it would be a good idea to look elsewhere.

Henry
 
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
reply
    Bookmark Topic Watch Topic
  • New Topic