2017年9月17日 | Leave a comment 问题: 解决: //CountDownLatchTest.java //javac CountDownLatchTest.java && java CountDownLatchTest import java.util.concurrent.*; class CountDownLatchTest{ public static void main(String[] argv){ CountDownLatch latch = new CountDownLatch(2); Thread t1 = new Thread(()->{ try{ latch.await(); System.out.println("t1 ok "); }catch(Exception e){} }); t1.start(); Runnable r = ()->{ try{ Thread.sleep(1000); System.out.println("countDown ok"); latch.countDown(); }catch(Exception e){} }; Thread t2 = new Thread(r); t2.start(); Thread t3 = new Thread(r); t3.start(); } } 1234567891011121314151617181920212223242526272829 //CountDownLatchTest.java//javac CountDownLatchTest.java && java CountDownLatchTest import java.util.concurrent.*; class CountDownLatchTest{ public static void main(String[] argv){ CountDownLatch latch = new CountDownLatch(2); Thread t1 = new Thread(()->{ try{ latch.await(); System.out.println("t1 ok "); }catch(Exception e){} }); t1.start(); Runnable r = ()->{ try{ Thread.sleep(1000); System.out.println("countDown ok"); latch.countDown(); }catch(Exception e){} }; Thread t2 = new Thread(r); t2.start(); Thread t3 = new Thread(r); t3.start(); }} 参考: