閉鎖做用是,在閉鎖到達結束以前,會阻止其餘線程經過,而到達結束的時候,則打開門,容許其他線程經過。java
是一種閉鎖實現,是經過計數的形式實現,能夠實現一個主線程須要依賴其餘多個線程完成後,再繼續 執行的功能。下面是本身寫的一個很簡單的demo:dom
public static void main(String[] args) { CountDownLatch counter = new CountDownLatch(3); for (int i = 0; i < 3; i++) { Thread thread = new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(5000); counter.countDown(); } catch (Exception e) { counter.countDown(); } } }); thread.start(); } System.out.printf("我在等待三個線程運行完\n"); try { counter.await(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.printf("我等完了三個線程運行"); }
就是至關於設置一道基線,當全部線程都到達這個基線的位置時候,才能繼續往下走。例子以下:ide
public static void main(String[] args) throws Exception { // 這裏Cyclic構造參數裏能夠傳遞一個Runnable,意思就是當全部線程都到達的時候,將會執行這個runnable CyclicBarrier cb = new CyclicBarrier(3, new Runnable() { @Override public void run() { System.out.printf("比賽如今開始~"); } }); Thread thread1 = new Thread(new Sports(cb, "中單")); Thread thread2 = new Thread(new Sports(cb, "上單")); Thread thread3 = new Thread(new Sports(cb, "打野")); thread1.start(); thread2.start(); thread3.start(); } public static class Sports implements Runnable { CyclicBarrier barrier = null; String name = null; Sports(CyclicBarrier cb, String name) { this.barrier = cb; this.name = name; } @Override public void run() { try { System.out.printf(name + "在準備中\n"); Thread.sleep(new Random().nextInt(10) * 1000); System.out.printf(name + "準備完畢,等待其餘位置\n"); barrier.await(); } catch (Exception e) {} } }
運行結果以下:this
FutureTask也能夠用來實現閉鎖的功能,由於FutureTask的是能夠返回結果的,當主線程去調用futureTask.get()的時候,會返回結果,若是線程尚未執行完畢,就會一直阻塞等待,直到拿到結果爲止,固然能夠設置等待的超時時間。spa