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> futures = executor.invokeAll(collection);
for(Future 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?
解决方案
To check if all are true, you can do something like this:
boolean works=true;
for(Future 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")