如何发出多个 SwingWorker 全部完成的信号?
我有以下问题,或者想做,我有 n 个正在运行的 SwingWorkers,数量可以在 1 到 10 之间变化。我从主线程启动它们,并以 n 个数量创建它们,然后让它们运行。当所有 n 个 SwingWorkers 完成后,我想做另一个任务,该任务基本上使用信息,SwingWorkers 处理并将它们全部连接到主线程中以对其进行处理。但要开始此任务,所有 n 个 SwingWorker 都需要完成/完成,并且所有 n 个 SwingWorker 都需要成功完成。
最好的方法是什么? Java 中是否已经有一种机制可以执行类似的操作,例如 ThreadManager,您可以将多个 SwingWorker 放入其中,然后向主线程触发 didAll() 或类似的操作?
主线程同时做其他事情,不能只是等待 n 个 SwingWorkers 完成。我需要一个“全部完成”的火灾事件。
我想在主线程中创建另一个线程,它运行一个 while 循环(直到AllSWFfinished),并在循环中等待 500 毫秒进行检查,但这对我来说似乎有点脏。
有没有更优雅的方法来实现这一目标?
I have the following issue, or want to do, that I have n running SwingWorkers, the number can vary between 1 and 10. I start them from a main thread, and create them in n numbers, then let them run. After all n SwingWorkers are done, I want to do another task, which basically uses information, the SwingWorkers processed and join them all in the main thread to do something with it. But for this task to begin, all n SwingWorkers need to be finished/done and all n SwingWorkers need to be finished successfully.
What would be the best way to do this? Is there a mechanism in Java already, which does something like this, like a ThreadManager, where you can put multiple SwingWorkers into, and then it fires a doneAll() or something like that to the main thread?
The main thread does other things in the meantime, and cant just wait for the n SwingWorkers to finish. I need an "all done" fire event somehow.
I though of creating another thread in the main thread, which runs a while loop (untillAllSWFfinished), with a wait 500ms in the loop to check, but that seems a bit dirty to me.
Is there a more elegant way to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您知道要解雇多少个工人,则可以使用 CountdownLatch。如果您不知道有多少作品正在启动,您可以使用 Phaser。
例子:
If you know how many workers you're kicking off, you can use a CountdownLatch. If you don't know how many works are being kicked off, you can use a Phaser.
Example: