问题:
解决:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
//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(); } } |
参考: