javaCountDownLatch闭锁
package com.java.concurrent; import java.util.concurrent.CountDownLatch; /** * CountDownLatch: 闭锁,在完成某些运算的时候,只有其它所有线程的运算全部完成后,当前运算才会执行 * @author fliay * */ public class TestCountDownLatch { public static void main(String[] args) { final CountDownLatch latch = new CountDownLatch(10); LactchDemo Ld = new LactchDemo(latch); long start = System.currentTimeMillis(); for (int i = 0; i < 5; i++) { new Thread(Ld).start(); new Thread(Ld).start(); } try { latch.await();//执行等待 } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } long end = System.currentTimeMillis(); System.out.println("消耗时间为:" + (end - start)); } } class LactchDemo implements Runnable { private CountDownLatch latch; public LactchDemo(CountDownLatch latch) { this.latch = latch; } public void run() { synchronized (this) { try { for (int i = 0; i < 10; i++) { if (i % 2 == 0) { System.out.println(i); } } } finally { // 启动闭锁 latch.countDown(); } } } }