ExecutorService JProgressBar

发布于 2024-12-11 12:06:42 字数 380 浏览 5 评论 0原文

这可能是一个简单的问题,但我尝试了很多不同的事情,无论我尝试什么,我都无法让它发挥作用。

基本上我有一个 JProgressBar,我想在处理任务列表时更新它。任务很短,但是数量很多,这就是我使用 ExecutorService 的原因。

问题是我找不到一个好的方法来监听 ExecutorService 还剩多少任务需要处理。 invokeAll() 会阻塞,直到所有任务完成,如果我使用 Submit() 执行每个任务,那么当它到达 JProgressBar 的代码时,任务几乎已经完成。我什至尝试将两者交织在一起,但这很糟糕。

有没有一种简单的方法来提交一批任务(实现可调用),然后调用开始处理的execute()方法?

或者我在这里看的方向完全错误?

谢谢!

This is probably a simple one, but I've tried loads of different things and no matter what I try, I can't get this to work.

Basically I have a JProgressBar that I want to update as I process the task list. The tasks are quite short, but there is a lot of them which is why I used an ExecutorService.

The problem is that I can't find a good way to listen to the ExecutorService for how many tasks are left to process. invokeAll() blocks until all the tasks are complete an if I use submit() to execute each task, the tasks are pretty much done by the time it hits the code for the JProgressBar. I even tried interleaving the two but that was just nasty.

Is there an easy way to submit a batch of tasks (implementing callable), then call an execute() method which starts processing in?

Or am I looking in completely the wrong direction here?

Thanks!

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

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

发布评论

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

评论(1

装迷糊 2024-12-18 12:06:42

您可以使用 ExecutorCompletionServicehttp://download.oracle.com/javase/1,5.0/docs/api/java/util/concurrent/ExecutorCompletionService.html

上的示例文档页面非常好;)

基本上,您只需将任务提交到服务,然后轮询或接受。任务返回后,您可以更新您的 JProgressbar

You could use an ExecutorCompletionService: http://download.oracle.com/javase/1,5.0/docs/api/java/util/concurrent/ExecutorCompletionService.html

The examples on the doc page are pretty good ;)

Basically you're just submitting the tasks to the service and then poll or take. After a task got back, you can update your JProgressbar.

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