[英]How can I create an “event-driven” background thread in Java?
I like the simplicity of invokeLater()
for sending units of work to the AWT EDT.我喜欢将工作单元发送到 AWT EDT 的
invokeLater()
的简单性。 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.最好有一个类似的机制来将工作请求发送到后台线程(例如 SwingWorker),但据我了解,这些没有任何类型的事件队列和调度机制,这正是 invokeLater() 所依赖的。
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?).事实上,这可能正是人们在后台线程中实现类似 EDT 行为的方式(或者会这样吗?)。 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"?
Java 是否提供了创建这种“事件驱动的工作线程”的方法? 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?与此相关的是,
invokeLater()
消息传递技术是否存在缺陷?
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; 生产者-消费者设计模式(这是您在阻塞队列中使用的)只是解决不同 class 问题的不同方法; EDT employs the Worker Design Pattern .
EDT 采用工人设计模式。 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.
因此,再次强调,选择设计模式是基于最适合您的特定情况的设计模式——当模式足够灵活以适应您想要的行为时,没有特别错误的选择。
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);
你应该看看 java.util.concurrent,更具体地说是在Executor的,它们通常只是一个可以处理这样的请求的线程池:
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()'
.如果您希望单个线程处理所有请求,请使用单个线程创建线程:
executor = Executors.newSingleThreadExecutor()'
。 There are also ExecutorService's which can return a value when the task is done.还有 ExecutorService 可以在任务完成时返回一个值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.