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

发布于 2024-11-29 21:01:18 字数 440 浏览 2 评论 0原文

我喜欢使用 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 技术交流群。

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

发布评论

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

评论(2

与往事干杯 2024-12-06 21:01:18

生产者-消费者设计模式(这就是您在阻塞队列中使用的)只是解决不同类的不同方法问题; EDT 采用工作器设计模式。查看这两种设计模式,看看哪一种最适合您的需求。

  • 当您有多个线程分别执行独立任务时,通常会使用生产者-消费者模式。
  • EDT 使用的 Worker 模式用于将多个任务的结果汇集到单个线程(在本例中为 GUI 线程)中。

当然,如果您有一个队列和一个具有多个生产者的消费者,您可以采用生产者-消费者模式并实现与工作模式类似的行为,但这只是突出了设计模式的灵活性。因此,重点是,选择设计模式应基于最适合您的特定情况的设计模式 - 当模式足够灵活以适应您想要的行为时,就没有特别错误的选择。

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.

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

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.

千仐 2024-12-06 21:01:18

您应该查看 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.

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