多线程应用程序中的事件处理
我正在用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用事件总线。
这是关于使用的演示 GWT 中的事件总线。它应该能让您很好地了解其好处(而且也很有趣)。
编辑
第一个链接主要作为示例给出。自己实现类似的适合您需求的东西实际上并不难。
Use an event bus.
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.
我会使用 ExecutorServices 来管理您的线程池。这样,当您有事件的侦听器时,您可以确保使用代理或手动编码将事件添加到正确的服务。例如,
您可以将此侦听器包装器传递为 并确保将使用所需的线程池处理该事件。
使用代理可以避免这种类型的样板代码。 ;)
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.
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.
使用事件总线绝对是正确的选择。有各种解决方案。您还可以查看 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
使用命令设计模式来解耦
use command design pattern to decoupling