Java 如何在所有线程完成后立即执行某项操作?
假设我希望有n个线程在运行,并且我希望在所有线程完成后输出一些东西。以下是我尝试过的方法:Java 如何在所有线程完成后立即执行某项操作?,java,multithreading,synchronization,Java,Multithreading,Synchronization,假设我希望有n个线程在运行,并且我希望在所有线程完成后输出一些东西。以下是我尝试过的方法: //This uses a ThreadGroup called tGroup while(tGroup.activeCount() > 0) { try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } } 接下来是一个while
//This uses a ThreadGroup called tGroup
while(tGroup.activeCount() > 0) {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
接下来是一个while循环和一个存储线程的ArrayList
boolean alive = true;
int count = 0;
while (alive) {
count = 0;
for (int i = 0; i < numThreads; i++) {
if (!threads.get(i).isAlive()) {
count++;
}
if (count == numThreads) {
alive = false;
break;
}
}
}
boolean-alive=true;
整数计数=0;
(活着时){
计数=0;
for(int i=0;i
循环所有线程,并join()
每个线程。join()。join()
将阻止任何未完成的线程,直到其完成。您的意思是:
boolean alive = true;
int count = 0;
while (alive) {
count = 0;
for (int i = 0; i < numThreads; i++) {
if (!threads.get(i).isAlive()) {
count++;
}
}
if (count == numThreads) {
alive = false;
}
}
boolean-alive=true;
整数计数=0;
(活着时){
计数=0;
for(int i=0;i
你是说:
boolean alive = true;
int count = 0;
while (alive) {
count = 0;
for (int i = 0; i < numThreads; i++) {
if (!threads.get(i).isAlive()) {
count++;
}
}
if (count == numThreads) {
alive = false;
}
}
boolean-alive=true;
整数计数=0;
(活着时){
计数=0;
for(int i=0;i
?我认为您想要使用的是a,因为它是专门为这种情况而构建的。每个工作线程将在锁存完成时通知锁存器,然后在锁存器上调用await()
的所有线程将保持操作,直到倒计时完成。请看我上面给出的API链接中的示例代码,看看这是多么容易和灵活
编辑:
哎呀,我想我发这个太晚了。但是,不管你接受了另一个答案,你还是应该亲自检查一下,因为它非常优雅且易于使用
例如:
import java.util.Random;
import java.util.concurrent.CountDownLatch;
public class CountDownLatchEg {
public static void main(String[] args) {
int threadCount = 8;
CountDownLatch latch = new CountDownLatch(threadCount);
System.out.println("Start all threads");
for (int i = 0; i < threadCount; i++) {
new Thread(new MyRunnable(latch, i)).start();
}
System.out.println("All threads started");
try {
latch.await();
} catch (InterruptedException e) {}
System.out.println("All threads finished");
}
}
class MyRunnable implements Runnable {
private CountDownLatch latch;
private Random rand = new Random();
private long delay;
private int id;
public MyRunnable(CountDownLatch latch, int id) {
this.latch = latch;
delay = (rand.nextInt(4) + 1) * 1000;
this.id = id;
}
@Override
public void run() {
System.out.println("Start thread: " + id);
try {
Thread.sleep(delay);
} catch (InterruptedException e) {}
System.out.println("End thread: " + id);
latch.countDown();
}
}
import java.util.Random;
导入java.util.concurrent.CountDownLatch;
公共类倒计时{
公共静态void main(字符串[]args){
int threadCount=8;
CountDownLatch闩锁=新的CountDownLatch(线程计数);
System.out.println(“启动所有线程”);
对于(int i=0;i
我认为您想要使用的是一个,因为它是专门为这种情况而构建的。每个工作线程将在锁存完成时通知锁存器,然后在锁存器上调用await()
的所有线程将保持操作,直到倒计时完成。请看我上面给出的API链接中的示例代码,看看这是多么容易和灵活
编辑:
哎呀,我想我发这个太晚了。但是,不管你接受了另一个答案,你还是应该亲自检查一下,因为它非常优雅且易于使用
例如:
import java.util.Random;
import java.util.concurrent.CountDownLatch;
public class CountDownLatchEg {
public static void main(String[] args) {
int threadCount = 8;
CountDownLatch latch = new CountDownLatch(threadCount);
System.out.println("Start all threads");
for (int i = 0; i < threadCount; i++) {
new Thread(new MyRunnable(latch, i)).start();
}
System.out.println("All threads started");
try {
latch.await();
} catch (InterruptedException e) {}
System.out.println("All threads finished");
}
}
class MyRunnable implements Runnable {
private CountDownLatch latch;
private Random rand = new Random();
private long delay;
private int id;
public MyRunnable(CountDownLatch latch, int id) {
this.latch = latch;
delay = (rand.nextInt(4) + 1) * 1000;
this.id = id;
}
@Override
public void run() {
System.out.println("Start thread: " + id);
try {
Thread.sleep(delay);
} catch (InterruptedException e) {}
System.out.println("End thread: " + id);
latch.countDown();
}
}
import java.util.Random;
导入java.util.concurrent.CountDownLatch;
公共类倒计时{
公共静态void main(字符串[]args){
int threadCount=8;
CountDownLatch闩锁=新的CountDownLatch(线程计数);
System.out.println(“启动所有线程”);
对于(int i=0;i
没有连接,所以在线程完成之前没有线程可以运行?如果是这样,这不是我想要的。我想让所有线程同时运行,当最后一个线程运行完成时,我想输出一些东西。@Backonhead-不,Ted是正确的。无论如何,所有启动的线程都将继续运行,不受此代码的干扰。当每个线程停止时,这将“打开”每个join()。一旦所有的门都打开了,你就可以继续你的“想要输出一些东西”语句了。为什么不直接用一个来代替呢?它不是专门为这种情况构建的吗?join()会导致当前线程等待被join()的线程完成。所以你的控制器线程(创造一个术语)可以开始,开始,开始,开始,然后加入其中的第一个。当连接后的语句完成时,第一个线程完成。您可以将其编码为join、join、join、join,最后一个join之后的语句将在所有线程都被执行时执行