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--");
	}

}

  

 

posted @ 2016-10-19 17:41  天空中的蜂蜂  阅读(192)  评论(0编辑  收藏  举报