Reputation: 35
I have this code creating threads. I need to know how to wait for it to finish before launching.
Map<String, String> accountsToRaidWith = Functions.getAccountsByCrew(accounts, crew);
for (Map.Entry<String, String> entry : accountsToRaidWith.entrySet()) {
BossRaiding R1 = new BossRaiding(entry.getKey(), entry.getValue(), raidid);
R1.start();
}
boolean launched = Functions.launchRaid(raidid, id);
class BossRaiding implements Runnable {
private Thread t;
String name;
String id;
String raidid;
BossRaiding (String name, String id, String raidid) {
this.name = name;
this.id = id;
this.raidid = raidid;
}
public void run() {
Functions.joinRaid(raidid, id);
}
public void start () {
if (t == null) {
t = new Thread (this);
t.start ();
}
}
}
I am trying to wait for all of the accounts to be joined before it launches. I can't seem to find a method that works correctly.
Upvotes: 0
Views: 109
Reputation: 103913
That Thread t
object has a method: join()
. This will freeze the thread that executes that method until t
stops.
Thus, you could make, in BossRaiding
:
public void join() throws InterruptedException {
t.join();
}
and then just loop through all your instances, calling join()
on all of them. If you get through that entire loop, every thread has completed.
Upvotes: 3