作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我必须 || 运行多个线程,并在执行完所有这些线程后主线程继续。比如我有1个主线程和3个子线程,我的需求是
run main thread
pause main thread
run all 3 sub threads ||ly
after complition resume main thread
我创建了一个类 extends Thread
并调用了所有这些线程的启动方法,但这并没有解决我的问题。
我的代码:
for (MyThread myThread : myThreads) {
myThread.start();
}
感谢您的帮助。
最佳答案
尝试使用 Thread.join();
public class ThreadDemo implements Runnable {
public void run() {
Thread t = Thread.currentThread();
System.out.print(t.getName());
//checks if this thread is alive
System.out.println(", status = " + t.isAlive());
}
public static void main(String args[]) throws Exception {
Thread t = new Thread(new ThreadDemo());
// this will call run() function
t.start();
// waits for this thread to die
t.join();
System.out.print(t.getName());
//checks if this thread is alive
System.out.println(", status = " + t.isAlive());
}
}
输出:
Thread-0, status = true
Thread-0, status = false
这是一个stack-over-flow link供引用。
关于java - 同时运行多个线程然后运行主线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18187066/
有人可以向我澄清主线 DHT 规范中的声明吗? Upon inserting the first node into its routing table and when starting up th
我正在尝试使用 USB 小工具驱动程序使嵌入式设备作为 MTP 设备工作。 我知道 Android 从大容量存储设备切换到 MTP 设备已经有一段时间了,并且找到了 source code for M
我是一名优秀的程序员,十分优秀!