码迷,mamicode.com
首页 > 编程语言 > 详细

java多线程编程之CountDownLatch

时间:2016-10-19 19:10:02      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/fengfengtk/p/5978174.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!