1

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!

PeterM
  • 2,534
  • 6
  • 31
  • 38

1 Answers1

1

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.

Thomas Jungblut
  • 20,854
  • 6
  • 68
  • 91
  • and the worst thing is I remembering seeing this once! Thanks for the wakeup ;) The only issue I see is that I still can't submit the tasks "en masse". I have to submit one by one... – PeterM Oct 22 '11 at 17:04
  • What about keeping a field which tells how much tasks have been submitted yet? – Thomas Jungblut Oct 22 '11 at 17:05
  • If you can't submit en masse, you can limit the number of processors running using Executors.newFixedThreadPool(nProcs). You can get the number of processors using Runtime.getRuntime().availableProcessors(); –  Oct 30 '11 at 03:00