简体   繁体   中英

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 (eg 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?

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.

Usage of an event bus is definitely the right choise. There are various solutions out there. You can also check out MBassador https://github.com/bennidi/mbassador .

It is annotation driven, very light-weight and uses weak references (thus easy to integrate in environments where objects lifecycle management is done by a framework like spring or guice or somethign). It provides an object filtering mechanism and synchronous or asynchronous dispatch/message handling. And it's very fast!

Google Guava has an event bus as well but it uses strong references which can be a pain if you do not have full control over your object lifecycle (eg spring environment)

EDIT: I created a performance and feature comparison for a selection of available event bus implementations including Guava, MBassador and some more. The results are quite interesting. Check it out here http://codeblock.engio.net/?p=37

I would use ExecutorServices to manage your thread pools. This way when you have a listener to an event, you can ensure the event is added to the right service either using a Proxy, or hande coded. eg

public void onEventOne(final Type parameter) {
    executorService.submit(new Runnable() {
        public void run() {
            wrappedListener.onEventOne(parameter);
        }
    }
}

You can pass this listener wrapper as and be sure the event will be processed using the desired thread pool.

Using a Proxy allows you to avoid this type of boiler plate code. ;)

Do you really need a solution where each thread can register as a listener for each type of event? If so, use an event bus type solution (or a centralized observable with typed events).

If you don't need this flexibility a manager-worker setup could suffice, where the manager gets notified of events (like: "I'm finished with my job") and can fire up workers as needed.

使用命令设计模式进行解耦

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM