Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Consider the following elements in an event-driven architecture:

  1. Observable Object
  2. Event Dispatcher
  3. Observer(Listener)

Now, we have two messages:

  1. From Observable Object to Event Dispatcher
  2. From Event Dispatcher to Observer(s).

How do you decide to pass these messages Sync or Async? Do you use any particular API (in Java) to implement that?

For example, in Java JFC/Swing, the first is Async and the second is Sync. The former is the reason that you can still fire more events even if the program is processing another event. The latter is the reason that you should avoid long operations in your listeners since they block the event dispatcher.

share|improve this question

A queue or priority queue from observable object to event dispatcher using Asyncronous message passing would be perfectly fine (due to the reasons you have already provided).

Unless you are using parallelism the event dispatcher should be Syncronous and even then it should have some sort of semaphore to make sure that no more than X number of observers (or instances of the same observer being careful around areas of mutex) are being pushed to; where X should coincide with the number of cores that you're running parallel on. Simple semaphores are fairly easy to implement in any language and I think Java has some system built in to implement them.

share|improve this answer

There is no hard-and-fast rule for this. Async delivery implies multiple threads, which increases the complexity of your solution, but also increases the flexibility. Synchronous might be better for cases where event handlers could benefit from sharing resources (e.g. a database connection), since the handlers would be executing on the same thread.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.