Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Ask Threads and Events..  RSS feed

 
Ricky Boy
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...
 
D Rog
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.
 
Stan James
(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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!