Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event handling in multithreaded application

I'm designing a stand-alone, multi-threaded application in Java. I'm trying to choose the best event-handling solution for his project.

I have 1-3 threads generating events (e.g comm thread completes file upload), while other threads might want to be registered for notification on this event. I want the event-generating and event listening to be as uncoupled as possible.

What do you suggest?

like image 548
user653952 Avatar asked Dec 16 '22 14:12

user653952


1 Answers

Use an event bus.

An event bus can be thought of as a replacement for the observer pattern, where in the observer pattern, each component is observing an observable directly. In the event bus pattern, each component simply subscribes to the event bus and waits for its event notification methods to be invoked when interesting events have occurred. In this way, an event bus can be thought of like the observer pattern with an extra layer of decoupling.

Here's a nice presentation about using an event bus ins GWT. It should give you a good idea about the benefits (and it's quite funny, too).

EDIT

The first link is mainly given as an example. It's really not that hard implementing something similar on your own which fits your needs.

like image 157
helpermethod Avatar answered Dec 27 '22 08:12

helpermethod