java多线程编程之CountDownLatch
Posted 天空中的蜂蜂
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java多线程编程之CountDownLatch相关的知识,希望对你有一定的参考价值。
java.util.concurrent.CountDownLatch这个类里面的主要方法为:
1.countDown(),Decrements the count of the latch, releasing all waiting threads if the count reaches zero.
2.await(),Causes the current thread to wait until the latch has counted down to zero, unless the thread is interrupted.
//Runner.java package com.cc; import java.util.concurrent.CountDownLatch; public class Runner implements Runnable{ private CountDownLatch latch; private int index; public Runner(int index){ this.index = index; } public Runner(CountDownLatch latch, int index){ this.latch = latch; this.index = index; } @Override public void run() { // TODO Auto-generated method stub try { Thread.sleep(10); latch.countDown(); System.out.println("latch countDown :"+this.index); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("latch countDown:"+this.index+"end"); } public CountDownLatch getLatch() { return latch; } public void setLatch(CountDownLatch latch) { this.latch = latch; } } //test.java package com.cc; import java.util.concurrent.CountDownLatch; import java.util.concurrent.Executors; import java.util.concurrent.ExecutorService; public class test { public static void main(String[] args) { // TODO Auto-generated method stub CountDownLatch latch = new CountDownLatch(10); ExecutorService execServ = Executors.newFixedThreadPool(10); for (int i = 0; i < 10; i++) { execServ.submit(new Runner(latch, i)); } try { Thread.sleep(1); latch.await(); System.out.println("latch await through pass"); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("--end--"); } }
//Runner.java package com.cc; import java.util.concurrent.CountDownLatch; public class Runner implements Runnable{ private CountDownLatch latch; private CountDownLatch backLatch; private int index; public Runner(int index){ this.index = index; } public Runner(CountDownLatch latch, CountDownLatch backLatch, int index){ this.latch = latch; this.backLatch = backLatch; this.index = index; } @Override public void run() { // TODO Auto-generated method stub try { System.out.println("latch countDown :"+this.index); latch.countDown(); System.out.println("latch:"+this.index); backLatch.await(); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } System.out.println("latch countDown:"+this.index+"end"); } public CountDownLatch getLatch() { return latch; } public void setLatch(CountDownLatch latch) { this.latch = latch; } public CountDownLatch getBackLatch() { return backLatch; } public void setBackLatch(CountDownLatch backLatch) { this.backLatch = backLatch; } } //Test.java package com.cc; import java.util.concurrent.CountDownLatch; import java.util.concurrent.Executors; import java.util.concurrent.ExecutorService; public class Test { public static void main(String[] args) { // TODO Auto-generated method stub CountDownLatch latch = new CountDownLatch(10); CountDownLatch backLatch = new CountDownLatch(1); ExecutorService execServ = Executors.newFixedThreadPool(10); for (int i = 0; i < 10; i++) { execServ.submit(new Runner(latch, backLatch,i)); } try { latch.await(); System.out.println("latch await through pass"); backLatch.countDown(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("--end--"); } }
以上是关于java多线程编程之CountDownLatch的主要内容,如果未能解决你的问题,请参考以下文章
Java并发多线程编程——并发工具类CountDownLatch(线程计数器)
Java多线程-两种常用的线程计数器CountDownLatch和循环屏障CyclicBarrier