If I make a join
for each of them, they will run in sequence, one at a time. I needed to start the 5 in parallel and know when they were all done to do something. Does anyone know if and how is it possible? Thank you in advance.
If I make a join
for each of them, they will run in sequence, one at a time. I needed to start the 5 in parallel and know when they were all done to do something. Does anyone know if and how is it possible? Thank you in advance.
Maybe what you want is something like this:
public class Main {
private static Thread criarThread(final int numero) {
return new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println("t" + numero + " começou");
Thread.sleep((int) (Math.random() * 10000));
System.out.println("t" + numero + " terminou");
} catch (InterruptedException e) {
// Ignora...
}
}
});
}
public static void main(String[] args) {
Thread[] ts = {
criarThread(1),
criarThread(2),
criarThread(3),
criarThread(4),
criarThread(5)
};
for (Thread t : ts) {
t.start();
}
for (Thread t : ts) {
try {
t.join();
} catch (InterruptedException e) {
// Ignora...
}
}
System.out.println("Todas as threads terminaram");
}
}
Look at one of the outputs I got:
t1 começou
t2 começou
t3 começou
t4 começou
t5 começou
t4 terminou
t1 terminou
t3 terminou
t5 terminou
t2 terminou
Todas as threads terminaram
Note that 5 threads are started in parallel on the main thread and the main thread gives the join
in all 5 threads. Thus, only when the 5 have been finalized does the main thread proceed.
@Victor's response, while correct, blocks the main thread that in the case of Android is not advisable.
The solution is to create a thread to execute the joins:
public class Main {
private static Thread criarThread(final int numero) {
return new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println("t" + numero + " começou");
Thread.sleep((int) (Math.random() * 10000));
System.out.println("t" + numero + " terminou");
} catch (InterruptedException e) {
// Ignora...
}
}
});
}
public static void main(String[] args) {
Thread[] ts = {
criarThread(1),
criarThread(2),
criarThread(3),
criarThread(4),
criarThread(5)
};
for (Thread t : ts) {
t.start();
}
new Thread(new Runnable() {
@Override
public void run() {
for (Thread t : ts) {
try {
t.join();
} catch (InterruptedException e) {
// Ignora...
}
}
System.out.println("Todas as threads terminaram");
}
}).start();
System.out.println("A main está livre");
}
}
Output:
t1 started
t2 started
t3 started
t4 started
t5 started
A main is free
t3 finished t5 finished t1 finished t4 finished t2 finished All threads are finished
See it working on ideone
You can use the getAllStackTraces()
method of the class Thread
to do this:
Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
To convert to an array of Threads
:
Thread[] threadArray = threadSet.toArray(new Thread[threadSet.size()]);
Or, you can also store the number of int
active in Threads
:
int quantidadeDeThreads = Thread.getAllStackTraces().keySet().size();