nurgasemetey
nurgasemetey

Reputation: 770

Guava EventBus and Singleton: Synchronization

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

Answers (2)

Basil Bourque
Basil Bourque

Reputation: 338624

EventBus Is Built For Concurrent Use

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

maaartinus
maaartinus

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

Related Questions