ExecutorService JProgressBar
这可能是一个简单的问题,但我尝试了很多不同的事情,无论我尝试什么,我都无法让它发挥作用。
基本上我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
ExecutorCompletionService
: http://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.htmlThe 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
.