gpt4 book ai didi

java - 如何在使用 run() 启动线程的 Java 中等待线程完成

转载 作者:行者123 更新时间:2023-11-30 08:45:14 25 4
gpt4 key购买 nike

我正在启动这些线程:

          ThreadingHDFSUsage HDFSUsage=new ThreadingHDFSUsage(dcaps);  
ThreadingRAMandContainers RAMandContainers=new ThreadingRAMandContainers(dcaps);
ThreadingCoreNodesHDFSUsage CoreNodesHDFSUsage=new ThreadingCoreNodesHDFSUsage(dcaps);
ThreadingApplicationMonitoring ApplicationMonitoring= new ThreadingApplicationMonitoring(dcaps);

在执行其他操作之前,我应该如何等待所有这些线程完成。

我的一个线程操作的示例线程类代码是:

public class ThreadingHDFSUsage extends Thread {

//private PhantomJSDriver driver;

private DesiredCapabilities dcaps;

public ThreadingHDFSUsage(DesiredCapabilities dcaps) {
// TODO Auto-generated constructor stub
this.dcaps = dcaps;
}

public void run(){
System.out.println("task HDFS Usage");

PhantomJSDriver driver = new PhantomJSDriver(dcaps);
try {
Thread.sleep(10000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println(".........HDFS Usage..........");
String OverallHDFSUsage[] = null;
try {
OverallHDFSUsage = HDFSUsage.getWebData(driver,"http://1.2.3.4:8888/dfshealth.html#tab-overview","//*[@id=\"tab-overview\"]/table[2]/tbody/tr[2]/td","");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String OverallHDFSUsage1 = OverallHDFSUsage[0];


}
}

同样,我有其他线程的相关代码。

那么,我该如何等待所有这 4 个线程操作完成?

最佳答案

再次加入()它们:

HDFSUsage.join();  
RAMandContainers.join();
CoreNodesHDFSUsage.join();
ApplicationMonitoring.join();

每个 join() 等待特定线程完成。

关于java - 如何在使用 run() 启动线程的 Java 中等待线程完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33397934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com