CountDownLatch和CyclicBarrier初步学习

 

CountDownLatch用法:

 

需要调用CountDownLatch的await()方法来等待计数器归零(线程全部签到)。调用CountDownLatch的countDown()方法让计数器减一(签到)

 

package com.tch.test.concurrent.test;

import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CountDownLatchTest {
	/** 线程数 */
	private int num = 3;
	private Random random = new Random();
	/** 开始运行的计数器 */
	private CountDownLatch begin = new CountDownLatch(1);
	/** 所有线程签到的计数器 */
	private CountDownLatch end = new CountDownLatch(num);
	private ExecutorService executor = Executors.newFixedThreadPool(5);
	
	public static void main(String[] args) {
		new CountDownLatchTest().test();
	}

	private void test() {
		try {
			doTask();
			System.out.println( "开始 ");
			//发出开始运行的信号
			begin.countDown();
			//主线程等待end计数器减到0,也就是所有线程都完成签到(end.countDown())
			end.await();
			System.out.println("结束 "+System.currentTimeMillis());
			executor.shutdown();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	private void doTask(){
		for(int i=0;i<num;i++){
			executor.execute(new Task(i));
		}
	}
	
	class Task implements Runnable{
		private int id;
		public Task(int id){
			this.id = id;
		}
		@Override
		public void run() {
			try {
				//等待begin的计数器减到0,导致当前线程进入阻塞状态
				begin.await();
				Thread.sleep(random.nextInt(1500));
				System.out.println("线程"+id+"结束"+System.currentTimeMillis());
				//向end计数器报到,end计时器减一
				end.countDown();
				//等待end计数器减到0,也就是等待所有线程都完成
				end.await();
				System.out.println("全部线程结束 "+System.currentTimeMillis());
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

}

 

 

 

 

CyclicBarrier用法:

 

调用CyclicBarrier的await()方法来签到 并且 等待其它线程全部签到完成。

 

package com.tch.test.concurrent.test;

import java.util.Random;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CyclicBarrierTest {
	private CyclicBarrier barrier;
	/** 线程数 */
	private int num = 3;
	private Random random = new Random();
	private ExecutorService executor = Executors.newFixedThreadPool(5);

	public static void main(String[] args) {
		new CyclicBarrierTest().test();
	}

	private void test() {
		barrier = new CyclicBarrier(num, new Runnable() {
			@Override
			public void run() {
				System.out.println("全部签到完成"+System.currentTimeMillis());
				System.out.println(barrier.getNumberWaiting()+"  "+barrier.getParties());
				//barrier.reset();
				System.out.println("--------------重新开始------------");
				doTask();
			}
		});
		doTask();
		barrier.reset();
		//executor.shutdown();
	}
	//barrier.reset();
	private void doTask(){
		for(int i=0;i<num;i++){
			executor.execute(new Task(i));
		}
	}
	
	class Task implements Runnable{
		private int id;
		public Task(int id){
			this.id = id;
		}
		@Override
		public void run() {
			try {
				Thread.sleep(random.nextInt(1500));
				System.out.println("线程"+id+"结束"+System.currentTimeMillis());
				barrier.await();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}

 

 

你可能感兴趣的:(Cyclicbarrier,CountDownLatch)