关于 CountDownLatch await() 方法的问题?
Problems about CountDownLatch await() method?
在我的程序中CountDownLatch await()方法会继续阻塞程序,CountDownLatch是这样写的,
是一个倒计时锁存器,当计数为零时触发三个Thread执行,并证明三个当cdAnswer减为0时
Thread已经执行完了,然后Main Thread去执行,但是在我的程序中,三个Thread才执行完
两个Main Thread都进行了,谁能帮帮我,感激不尽。
最下面是我的程序。
public class CountdownLatchTest {
public static void main(String[] args) {
ExecutorService service = Executors.newCachedThreadPool();
final CountDownLatch cdOrder = new CountDownLatch(1);
final CountDownLatch cdAnswer = new CountDownLatch(3);
for (int i = 0; i < 3; i++) {
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
System.out.println("Thread" + Thread.currentThread().getName()
+ "Wait for the command");
cdOrder.await();
System.out.println("Thread" + Thread.currentThread().getName()
+ "received command");
Thread.sleep((long) (Math.random() * 10000));
System.out.println("Thread" + Thread.currentThread().getName()
+ "Feedback the results");
cdAnswer.countDown();
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("To complete the order");
cdAnswer.countDown();
}
}
};
service.execute(runnable);
}
try {
Thread.sleep((long) (Math.random() * 10000));
System.out.println("Thread" + Thread.currentThread().getName()
+ "The upcoming orders");
cdOrder.countDown();
System.out.println("Thread" + Thread.currentThread().getName()
+ "Sent the command, are waiting for the result");
cdAnswer.await();
System.out.println("Thread" + Thread.currentThread().getName()
+ "Have received all the response. "
+ "The results of this task has been completed");
} catch (Exception e) {
e.printStackTrace();
}
service.shutdown();
}
}
问题补充:
这个结果出乎我的意料,我以为这句话‘Have received all the response.这个任务的结果已经完成'会在最后打印出来
图片显示了我的程序的结果。
这是因为如果您看到 运行 方法 运行nable
public void run() {
try {
System.out.println("Thread"
+ Thread.currentThread().getName()
+ "Wait for the command");
cdOrder.await();
System.out.println("Thread"
+ Thread.currentThread().getName()
+ "received command");
Thread.sleep((long) (Math.random() * 10000));
System.out.println("Thread"
+ Thread.currentThread().getName()
+ "Feedback the results");
cdAnswer.countDown(); -- countdown on latch cdAnswer
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("To complete the order");
cdAnswer.countDown(); -- countdown on latch cdAnswer
}
}
cdAnswer.countDown();每个 运行nable 在 finally 和 1 before catch 子句中被调用两次。
因此,对于三个线程,倒计时被调用六次,结果主线程在 3 倒计时后启动。
移除cdAnswer.countDown();就在 catch 语句之上,它按预期工作
在我的程序中CountDownLatch await()方法会继续阻塞程序,CountDownLatch是这样写的, 是一个倒计时锁存器,当计数为零时触发三个Thread执行,并证明三个当cdAnswer减为0时 Thread已经执行完了,然后Main Thread去执行,但是在我的程序中,三个Thread才执行完 两个Main Thread都进行了,谁能帮帮我,感激不尽。 最下面是我的程序。
public class CountdownLatchTest {
public static void main(String[] args) {
ExecutorService service = Executors.newCachedThreadPool();
final CountDownLatch cdOrder = new CountDownLatch(1);
final CountDownLatch cdAnswer = new CountDownLatch(3);
for (int i = 0; i < 3; i++) {
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
System.out.println("Thread" + Thread.currentThread().getName()
+ "Wait for the command");
cdOrder.await();
System.out.println("Thread" + Thread.currentThread().getName()
+ "received command");
Thread.sleep((long) (Math.random() * 10000));
System.out.println("Thread" + Thread.currentThread().getName()
+ "Feedback the results");
cdAnswer.countDown();
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("To complete the order");
cdAnswer.countDown();
}
}
};
service.execute(runnable);
}
try {
Thread.sleep((long) (Math.random() * 10000));
System.out.println("Thread" + Thread.currentThread().getName()
+ "The upcoming orders");
cdOrder.countDown();
System.out.println("Thread" + Thread.currentThread().getName()
+ "Sent the command, are waiting for the result");
cdAnswer.await();
System.out.println("Thread" + Thread.currentThread().getName()
+ "Have received all the response. "
+ "The results of this task has been completed");
} catch (Exception e) {
e.printStackTrace();
}
service.shutdown();
}
}
问题补充: 这个结果出乎我的意料,我以为这句话‘Have received all the response.这个任务的结果已经完成'会在最后打印出来
图片显示了我的程序的结果。
这是因为如果您看到 运行 方法 运行nable
public void run() {
try {
System.out.println("Thread"
+ Thread.currentThread().getName()
+ "Wait for the command");
cdOrder.await();
System.out.println("Thread"
+ Thread.currentThread().getName()
+ "received command");
Thread.sleep((long) (Math.random() * 10000));
System.out.println("Thread"
+ Thread.currentThread().getName()
+ "Feedback the results");
cdAnswer.countDown(); -- countdown on latch cdAnswer
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("To complete the order");
cdAnswer.countDown(); -- countdown on latch cdAnswer
}
}
cdAnswer.countDown();每个 运行nable 在 finally 和 1 before catch 子句中被调用两次。 因此,对于三个线程,倒计时被调用六次,结果主线程在 3 倒计时后启动。
移除cdAnswer.countDown();就在 catch 语句之上,它按预期工作