R
R
reus2017-03-31 11:18:50
Java
reus, 2017-03-31 11:18:50

How to find out if all threads (Thread) have completed?

I start threads through a for loop:

MyThrowClass throw;
for(String i: all) {
  trow = new MyThrowClass(i);
  trow.start()

The thread executes the code and simply passes the result to a static container (based on ArrayList)
In order to check the number of elements of the container, I have the .getLength() method.
MyContainer.getLength();
But if you call this method immediately after for, the result will be null ( "0"), since threads have not completed their work yet.
Actually, how to find out if all the threads have completed their work in order to further transfer the result of their work further?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
kodwi, 2017-03-31
@kodwi

in a loop through the threads, call join () for each, after which you already write code that should work only after the completion of all threads.

Y
Ytsu Ytsuevich, 2017-03-31
@kofon

trow.isAlive();
And if you need to wait until the thread is finished:
trow.join();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question