Reputation: 770
I have a question regarding the principle of work of Guava EventBus. My objects are registered to EventBus. In 10 second time interval, message is sent to them, where processing is done and I compare some variable in object with singleton value.By some criteria, I change singleton value. I made some research in Guava documentation but I didn't find any information about synchronization issues.
Is this right way to do so?
With regards
Upvotes: 0
Views: 1484
Reputation: 338624
From the EventBus source code, line 91:
This class is safe for concurrent use.
I too was concerned about concurrency because the doc makes no mention. After perusing the source code, seeing this comment plus the use of concurrent collections has erased my concerns.
volatile
Regarding the Question's mention of comparing to a singleton's changing value across threads, you may need to use the volatile
keyword to protect access, to guarantee visibility of current value rather than old value cached in a processor core. But that is a regular concurrency concern, unrelated to use of the EventBus.
Upvotes: 1
Reputation: 46422
As long as you use EventBus
(rather than AsyncEventBus
), there are no synchronization issues added by the bus. The event bus simply executes you subscribers immediately in the same thread.
As long as you yourself stick with a single thread, there's no multithreading and no need for any synchronization.
Upvotes: 2