Question

Is there any way to find the number of tasks completed after a call to invokeAll()? It seems that that returns the list of booleans of completed tasks when all of the threads are completed.

I have a pool of 1000 tasks and want to take a look at them in 100 intervals without having to divide the them into 100-task batches.

I also for compatibility reasons have to work with Java 6 so newer methods won't help.

Also, as a side question: does the invokeAll() processes the tasks in FIFO manner? That is, are the tasks get started with the order with which they are added to the task list?

Thanks

Était-ce utile?

La solution

I have a pool of 1000 tasks and want to take a look at them in 100 intervals without having to divide the them into 100-task batches.

You should consider using an ExecutorCompletionService which allows you to get notified once a single job has finished instead of having to wait for all jobs to complete using invokeAll(). Then you can put each of the finished jobs into a collection and then act on them when you get 100.

Maybe something like:

CompletionService<Result> ecs = new ExecutorCompletionService<Result>(executor);
for (Callable<Result> s : solvers)
    ecs.submit(s);
int n = solvers.size();
List<Result> batch = new ArrayList<Result>();
for (int i = 0; i < n; ++i) {
    Result r = ecs.take().get();
    batch.add(r);
    if (batch.size() >= 100) {
       process(batch);
       batch.clear();
    }
}
if (!batch.isEmpty()) {
    process(batch);
}

does the invokeAll() processes the tasks in FIFO manner? That is, are the tasks get started with the order with which they are added to the task list?

The tasks are submitting to the thread-pool in FIFO manner and are dequeued by the threads also in FIFO order. However, once each thread has a job, there are race conditions which may cause some re-ordering of the actual task "start" and certainly finish.

Autres conseils

Tasks will be added in the order you specify, and approximately started in that order if you have multiple threads. The order they are completed will be roughly in that order if they take the same amount of time.

I would build a List<Future> which you can poll periodically to see how many are done.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top