多线程应用程序中的事件处理

发布于 2024-11-03 02:56:52 字数 154 浏览 0 评论 0原文

我正在用 Java 设计一个独立的多线程应用程序。 我正在尝试为他的项目选择最佳的事件处理解决方案。

我有 1-3 个线程生成事件(例如,通讯线程完成文件上传),而其他线程可能希望注册以获得有关此事件的通知。 我希望事件生成和事件侦听尽可能分离。

你有什么建议?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

情释 2024-11-10 02:56:52

使用事件总线

事件总线可以被认为是
替换观察者模式,
在观察者模式中,每个
组件正在观察一个可观察的
直接地。在事件总线模式中,
每个组件只需订阅
事件总线并等待其事件
要调用的通知方法
当有趣的事件发生时。
这样,事件总线就可以是
像观察者模式一样思考
具有额外的解耦层。

这是关于使用的演示 GWT 中的事件总线。它应该能让您很好地了解其好处(而且也很有趣)。

编辑

第一个链接主要作为示例给出。自己实现类似的适合您需求的东西实际上并不难。

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.

笑红尘 2024-11-10 02:56:52

我会使用 ExecutorServices 来管理您的线程池。这样,当您有事件的侦听器时,您可以确保使用代理或手动编码将事件添加到正确的服务。例如,

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

您可以将此侦听器包装器传递为 并确保将使用所需的线程池处理该事件。

使用代理可以避免这种类型的样板代码。 ;)

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. e.g.

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. ;)

白昼 2024-11-10 02:56:52

您是否真的需要一个每个线程都可以注册为每种类型事件的侦听器的解决方案?如果是这样,请使用事件总线类型解决方案(或带有类型化事件的集中式可观察对象)。

如果您不需要这种灵活性,经理-工人设置就足够了,经理会收到事件通知(例如:“我完成了我的工作”),并且可以根据需要解雇工人。

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.

木有鱼丸 2024-11-10 02:56:52

使用事件总线绝对是正确的选择。有各种解决方案。您还可以查看 MBassador https://github.com/bennidi/mbassador

它是注释驱动的,非常轻量级,并且使用弱引用(因此很容易集成到由 spring 或 guice 等框架完成对象生命周期管理的环境中)。它提供了对象过滤机制和同步或异步调度/消息处理。而且速度非常快!

Google Guava 也有一个事件总线,但它使用强引用,如果您不能完全控制对象生命周期(例如 spring 环境),这可能会很痛苦

编辑:我为选择的可用事件总线创建了性能和功能比较实现包括 Guava、Massador 等。结果非常有趣。在这里查看
http://codeblock.engio.net/?p=37

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 (e.g. 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

浅笑轻吟梦一曲 2024-11-10 02:56:52

使用命令设计模式来解耦

use command design pattern to decoupling

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文