Java Swingworker 和多线程

发布于 2025-01-04 08:50:41 字数 479 浏览 2 评论 0原文

我正在设计一个 Java GUI 驱动的应用程序,该应用程序运行许多单独的任务,每个任务都在其自己的 SwingWorker 扩展类中。这是我使用的正常设计,以便在自己的线程上运行任务,并仍然保持 EDT 空闲以更新 GUI。每个 SwingWorker 都使用 Executors.newCachedThreadPool 在自己的线程上启动。

然而,在一个特定的班级中,有一项任务需要相当长的时间来处理。该任务包含一个 for 循环,最多执行六次某些计算。

我曾想过在自己的线程中实现六个计算中的每一个,以加快处理时间,但我不确定实现此目的的最佳方法。

是否可以扩展 SwingWorker 并实现 Runnable,然后在 for 循环中使用 void Run() 方法,每次启动一个新线程,或者使用 cachedThreadPool。

或者我最好只使用标准 Thread() 实现?

任何意见或建议将不胜感激。

预先感

谢乔什

I'm in the process of designing a Java GUI driven application that runs a number of separate tasks, each within its own SwingWorker extended class. This is the normal design I use in order to run tasks on their own threads and still keep the EDT free to update the GUI. Each SwingWorker is started on its own thread using an Executors.newCachedThreadPool.

However, within one particular class, there is a task that requires quite a long time to process. The task contains a for loop that performs some calculations up to six times.

I've had the thought of implementing each of the six calculations within their own thread to speed up processing time, but I'm not sure of the best way of implementing this.

Is it possible to both extend SwingWorker and implement Runnable, and then use a void Run() method within the for loop, starting a new Thread each time, or using a cachedThreadPool.

Or am I better off just using standard Thread() implementation?

Any advice or suggestions would be appreciated.

Thanks in advance

Josh

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

温馨耳语 2025-01-11 08:50:41

user988052 当然是对的 - 如果您没有多核 CPU,则在六个不同的线程中进行计算实际上会减慢您的应用程序的速度,因为创建和管理线程会产生开销。

如果您想在 6 个单独的线程中执行此计算,您可以使用 SwingWorker 作为管理线程并在其中生成 5-6 个新线程。使用 ExecutorService 将是一种可行的方法,因为它使用线程池,从而最大限度地减少创建和处置线程所带来的开销。

编辑:回答您的评论:

我认为最好的方法是在 Runnable 中实现您的计算(以便六个计算中的每一个都可以单独运行),然后只需使用 ExecutorService< /code>,实例化您的 Runnable 六次并使用 ExecutorService.submit(Runnable task)。您可以在 SwingWorker.doInBackground() 方法中完成所有这些操作。

您永远不应该自己调用 run() 方法 - 让该操作由 Thread/ExecutorService 来完成。

user988052 is right of course - if you don't have a multi core CPU, doing the computation in six different threads will actually slow down your application because of the overhead that comes with the creation and administration of threads.

If you want to do this calculations in six separate threads nevertheless, you could use a SwingWorker as the managing thread and spawn off 5-6 new threads within it. Using ExecutorService would then be the way to go, since it uses a pool of threads and thus minimizes the overhead that comes with the creation and disposal of a thread.

Edit: to answer your comment:

I think the best way would be the implement your calculation in a Runnable (so that each of the six calculations can run separately), then simply use an ExecutorService, instantiate your Runnablesix times and use ExecutorService.submit(Runnable task). You can do all of this within the SwingWorker.doInBackground() method.

You should never call the run() method yourself - let that be done by a Thread/ExecutorService.

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