Executorservice with invokeall() and future in Java

Master the following codes:

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? If all the future If isdone() is true, then all threads have completed? How do I make a flag to make sure everything is done?

Solution

To check that everything is correct, you can do this:

boolean works=true;
  for(Future<Boolean> future : futures){
        future.get();
        if (future.isDone()) {
            System.out.println("true");
        }
        else{
            System.out.println("false");works=false;
        }
    }
if(works)System.out.println("yea it works")
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>