Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Question on wait() and notify()  RSS feed

 
Sree Nivas
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a doubt regarding wait() and notify(). Say t1,t2,t3...tn etc., threads invoked synchronized methods like m1,m2,m3..mn etc. and which are waiting for some thread notification. When one thread like t1 theread says notify() which thread will be notified either t2 or t3 ... tn etc., Whether it follows any order. I think there is no way to notify a particular thread. How to make sure only one wanted thread to be notified.
regards
Sreenivas
 
Pierre Henry
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sree,
don't forget wiat() and notify() both are methods of Object' class.
So let's s1 a member of t1 ans s2 a member of t2
in t1 you have: s1.wait(); -> t1 waits
in t2: s1.notify(); -> t2 runs again


 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java ti java tipa --
We don't have too many rules round these parts, but please check out our naming policy which prohibits one from having obviously fictitious display names. You can go here to update yours. Thanks, pardner!
Sreenivasa Rao Iruvanti -
Yes, your observation is correct. There's no qway to notify a particular thread. If several threads are waiting on a single monitor, any one of them might be awoken by a call to notify().
Trying to notify one particular thread out of such a group would introduce strong coupling between the threads in the application, generally a bad idea. In practice, the inability to notify a certain thread is really not a problem.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!