Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExecutorService with invokeAll() and Future in java

Tags:

java

Having the following code at hand:

    ExecutorService executor = Executors.newFixedThreadPool(10);
    Collection collection = new ArrayList();
    for (int n=1; n<100; n++)
        collection.add(new MyThread(n));

    try {
        List<Future<Boolean>> futures = executor.invokeAll(collection);

        for(Future<Boolean> future : futures){
            future.get();
            if (future.isDone()) {
                System.out.println("true");
            }
            else
                System.out.println("false");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

If the above is correct?
And if all future.isDone() are true, then all of the threads have been done?
How can I make a flag, to be sure that all of them are done?

like image 759
fen1ksss Avatar asked Oct 15 '12 13:10

fen1ksss


1 Answers

Meant as a comment on vainolo's reply but I don't have enough reputation points:

isDone is also redundant because invokeAll returns a list of futures for which isDone is true. The javadocs mention this.

like image 89
user1372408 Avatar answered Nov 08 '22 15:11

user1372408