How to ensure all threads are executed?

0

How can I ensure that the Get method of a FutureTask will only be called when all threads have already executed?

I have this method:

for (int j = 0; j < threadNum; j++) {
        FutureTask<Integer> futureTask = taskList.get(j);
        if(!taskList.get(j).isDone()){
            System.out.println("Não terminou: ");
        }
        amount += futureTask.get();

    }

Should I make an infinite loop before this is to ensure that it will only arrive here when all threads finish running or is there another way to do this?

    
asked by anonymous 20.01.2015 / 16:18

1 answer

1

Using FutureTask you do not need a loop to check if it's already finished. You also do not need a specific method to wait to finish. The get method itself does both - it waits to finish and then gets the computed value.

Your code would look something like this:

for (FutureTask<Integer> futureTask : taskList) {
    amount += futureTask.get();
}
    
20.01.2015 / 17:12