简体   繁体   English

如何在 Java 中创建“事件驱动”的后台线程?

[英]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.看看这两种设计模式,看看哪一种最适合您的需求。

  • The Producer-Consumer pattern is generally used when you have multiple threads executing independent tasks separately.当您有多个线程分别执行独立任务时,通常使用生产者-消费者模式。
  • The Worker pattern, employed by the EDT, is used to funnel the result of multiple tasks into a single thread (in this case the GUI thread). EDT 采用的 Worker 模式用于将多个任务的结果集中到单个线程(在本例中为 GUI 线程)。

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.

相关问题 如何避免事件驱动中的繁忙 while 循环 Java - How to avoid a busy while loop in event-driven Java 如何在 Java 中实现日历事件驱动的工作流 - How to implement a calendar event-driven workflow in Java 如何使用 Postgres 数据库在 Java 中获得异步/事件驱动的 LISTEN/NOTIFY 支持? - How do I get asynchronous / event-driven LISTEN/NOTIFY support in Java using a Postgres database? 使用 Java 和 SQL 服务器进行事件驱动编程 - Event-driven programming with Java and SQL Server 如何解耦事件驱动的模块? - How to decouple an event-driven module? 是否有针对Java的事件驱动的JSON REST客户端API? - Is there an event-driven JSON REST client API for Java? 如何在事件驱动的通信中表示事件数据? - how to represent event data in event-driven communication? 事件驱动的编程-node.js,Java - Event-driven programming - node.js, Java 我应该使用什么样的技术来创建事件驱动应用程序(Vert.x / Spring / Axon)? [等候接听] - What kind of technologies should I use to create Event-Driven application (Vert.x / Spring / Axon)? [on hold] 实施事件驱动的轻量级线程 - Implementing event-driven lightweight threads
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM