This week's book giveaway is in the Java in General forum.
We're giving away four copies of Helidon Revealed: A Practical Guide to Oracle’s Microservices Framework and have Michael Redlich on-line!
See this thread for details.
  • 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
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Thread communication

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nitinram,

We can not do so.
m i right guys?
 
Marshal
Posts: 80089
412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Umesh Vajpai wrote:m i right guys?

Not "m": please read this.
 
Campbell Ritchie
Marshal
Posts: 80089
412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Too difficult a topic for "beginning". Moving thread.
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 443
3
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Everyone is a villain in someone else's story. Especially this devious tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic