• Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread communication  RSS feed

 
nitinram agarwal
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
using wait and notify/notifyall, we normally communicate between threads. however my requirement is to notify a particular thread. how do I do it?
 
Umesh Vajpai
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nitinram,

We can not do so.
m i right guys?
 
Campbell Ritchie
Marshal
Posts: 55781
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Umesh Vajpai wrote:m i right guys?
Not "m": please read this.
 
Campbell Ritchie
Marshal
Posts: 55781
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Too difficult a topic for "beginning". Moving thread.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by 'notify a particular thread' ? if you have the thread instance, you can call notify on it, just as on any other object.
 
nitinram agarwal
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am talking about the wait/notify scenario. when there are a number of threads waiting for one thread to complete execution. how can we be sure that only a particular thread is notified.
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note the thread you want to wake up (careful how you do this re happens before ordering) in the completing thread somewhere that all threads can read or better still on the object being synchronized on, notify all and have all the notified threads test for if they are the one that should wake up and if not re wait, you could embed all this logic in the object being synchronized on.

 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nitinram agarwal wrote:I am talking about the wait/notify scenario. when there are a number of threads waiting for one thread to complete execution. how can we be sure that only a particular thread is notified.


I think, you can't do this!
 
amitabh mehra
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not directly.. but in implementation I think that this could be achieved... just an idea..
Use one lockObject ( create a class with some private member which will hold the next thread's name .. usual getter and setter methods)
Before starting all threads, set this member value to a given thread name, say "Thread 2"

In the run method, we will check if the current thread name is "Thread 2" then let it continue with its work else make it wait.
After completing its work, Thread 2 would set the member to next thread name... and then notify all.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!