[英]How do I wait for all of the threads to finish
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.
我似乎找不到正确工作的方法。
That Thread t
object has a method: join()
.该
Thread t
object 有一个方法: join()
。 This will freeze the thread that executes that method until t
stops.这将冻结执行该方法的线程,直到
t
停止。
Thus, you could make, in BossRaiding
:因此,您可以在
BossRaiding
中:
public void join() throws InterruptedException {
t.join();
}
and then just loop through all your instances, calling join()
on all of them.然后遍历所有实例,在所有实例上调用
join()
。 If you get through that entire loop, every thread has completed.如果您完成整个循环,则每个线程都已完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.