如何创建“事件驱动”的项目? Java 中的后台线程?
我喜欢使用 invokeLater()
将工作单元发送到 AWT EDT 的简单性。如果有一个类似的机制来向后台线程(例如 SwingWorker)发送工作请求,那就太好了,但据我了解,这些机制没有任何类型的事件排队和事件队列。调度机制,这是invokeLater()所依赖的。
因此,我最终给了我的后台线程一个阻塞队列,其他线程向该队列发送消息,并且该线程本质上运行一个接收循环,阻塞直到消息到达。
事实上,这可能正是人们在后台线程中实现类似 EDT 的行为的方式(或者会吗?)。另一方面,我喜欢线程的简单性,它只是惰性地悬挂在那里,每当“工作液滴”碰巧从天空中某个看不见的事件调度队列调度到它时,就对其进行处理。 Java 是否提供了创建这种“事件驱动的工作线程”的方法?或者消息队列到底是做到这一点的正确方法吗?与此相关的是,invokeLater()
消息传递技术有缺点吗?
I like the simplicity of invokeLater()
for sending units of work to the AWT EDT. It would be nice to have a similar mechanism for sending work requests to a background thread (such as SwingWorker) but as I understand it, these do not have any sort of event queueing & dispatch mechanism, which is what invokeLater() depends upon.
So instead, I've ended up giving my background thread a blocking queue, to which other threads send messages, and the thread essentially runs a receive loop, blocking until a message arrives.
That, in fact, might be exactly how one would implement EDT-like behavior in a background thread (or would it?). On the other hand, I like the simplicity of a thread that simply dangles there inertly, processing "work droplets" whenever they happen to be dispatched to it from some unseen Event Dispatching Queue in the sky. Does Java provide a way to create such an "event-driven worker thread"? Or is message-queueing the right way to do this, after all? And in a related vein, are there drawbacks to the invokeLater()
technique of message-passing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
生产者-消费者设计模式(这就是您在阻塞队列中使用的)只是解决不同类的不同方法问题; EDT 采用工作器设计模式。查看这两种设计模式,看看哪一种最适合您的需求。
当然,如果您有一个队列和一个具有多个生产者的消费者,您可以采用生产者-消费者模式并实现与工作模式类似的行为,但这只是突出了设计模式的灵活性。因此,重点是,选择设计模式应基于最适合您的特定情况的设计模式 - 当模式足够灵活以适应您想要的行为时,就没有特别错误的选择。
The Producer-Consumer Design Patter (which is what you're employing with your blocking queue) is just a different approach to solving a different class of problems; EDT employs the Worker Design Pattern. Take a look at both design patterns and see which one suits your needs best.
Of course, you can take the Producer-Consumer pattern and achieve similar behavior to the Worker pattern if you have a single queue and a single consumer with multiple producers, but that just highlights the flexibility of design patterns. So the point, again, is that choosing a design pattern is based on what works best for your particular situation- there is no particularly wrong choice when the patterns are flexible enough to accommodate the behavior you desire.
您应该查看 java.util.concurrent,更具体地说是 Executor,通常只是一个线程池,可以处理如下请求:
executor.execute(runnableTask);
。如果您希望单个线程处理所有请求,请使用单个线程创建线程:executor = Executors.newSingleThreadExecutor()'
。还有 ExecutorService 可以在任务完成时返回一个值。You should take a look at java.util.concurrent, more specifically at Executor's, which are usually just a thread pool that can process a request like this:
executor.execute(runnableTask);
. If you want a single thread to process all the request, then create your thread with a single thread:executor = Executors.newSingleThreadExecutor()'
. There are also ExecutorService's which can return a value when the task is done.