This week's book giveaways are in the Scala and Android forums.
We're giving away four copies each of Machine Learning Systems: Designs that scale and Xamarin in Action: Creating native cross-platform mobile apps and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Ask Threads and Events..  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi everyone, i'm newbie here and with java..
i wanna ask about object that can raise an event.
i have 1 object that has 1 variable which can be changed (public).
if i have 5 threads that access the same variable in this object, and one of them change the variable, can i raise an event, so the other threads know that the value of the variable was changed, and they can do some action?
ok, thx b4.
n sorry if my english make confusing...
 
Ranch Hand
Posts: 472
Linux Objective C Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can, but all other threads should wait for your event so certainly idling. Better solution could be that threads just checking some flag when they need to know if your variable was changed.
 
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your other threads keep working while waiting for the value to change you could look into Observer and Observable. Maybe make a single owner of the "gold data" that all threads listen to for changes.

If your other threads block until the value changes, look at wait() and notifyAll() on Object. All the threads would wait() on the same object and whoever changes it would notify the others.
 
Trust God, but always tether your camel... to this tiny ad.
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!