2
If I make one join
for each of them, they will perform in sequence, one at a time. I needed to start the 5 in parallel and know when they all finished to do something. Does anyone know if and how it’s possible? I thank you already.
2
If I make one join
for each of them, they will perform in sequence, one at a time. I needed to start the 5 in parallel and know when they all finished to do something. Does anyone know if and how it’s possible? I thank you already.
5
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 exits 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 the 5 threads are started in parallel on the main thread and the main thread gives the join
in the 5 threads. Thus, only when the 5 threads have been completed does the main thread continue.
3
The @Victor response, although correct, blocks the thread which, in the case of Android, is not advisable.
The solution is to create a thread to run 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 began
t2 began
T3 started
T4 began
T5 began
Main is free
T3 finished
T5 has finished
t1 has finished
T4 has finished
t2 is over
All threads are finished
See working on ideone
Very pertinent observation of not blocking the main thread.
0
You can use the method getAllStackTraces()
of one’s own class Thread
for that reason:
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 in a int
the number of Threads
active:
int quantidadeDeThreads = Thread.getAllStackTraces().keySet().size();
Browser other questions tagged java android thread
You are not signed in. Login or sign up in order to post.
I didn’t know this ideone. Very good!
– NilsonUehara