线程协作CountDownLatch&CyclicBarrier

using CountDownLatch&CyclicBarrier

最近在开发计算量较大的模块中使用到的线程协作总结

需求

要求三个线程按下图所示的流程协同执行

实现

public class ThreadDemo {
	public static void main(String[] args) {
		final CountDownLatch countDownLatch = new CountDownLatch(2);
		final CyclicBarrier cyclicBarrier = new CyclicBarrier(2);
		Thread threadA = new Thread(new worker("A", countDownLatch, cyclicBarrier));
		Thread threadB = new Thread(new worker("B", countDownLatch, cyclicBarrier));
		threadB.start();
		threadA.start();
		try {
			countDownLatch.await();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("All worke done");
	}

	public static void threadSleep() {
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

	}
}

class worker implements Runnable {
	private String workerNmae;
	private CountDownLatch countDownLatch;
	private CyclicBarrier cyclicBarrier;

	public worker(String workerNmae, CountDownLatch countDownLatch, CyclicBarrier cyclicBarrier) {
		this.workerNmae = workerNmae;
		this.countDownLatch = countDownLatch;
		this.cyclicBarrier = cyclicBarrier;
	}

	public void run() {
		System.out.println(this.workerNmae + " start work");
		ThreadDemo.threadSleep();
		try {
			this.cyclicBarrier.await();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (BrokenBarrierException e) {
			e.printStackTrace();
		}
		ThreadDemo.threadSleep();
		System.out.println(this.workerNmae + " end work");
		countDownLatch.countDown();
	}
}

区别

不难看出,这两个对象使用的时机、方式有所不同,CyclicBarrier的await使工作线程阻塞;CountDownLatch的await在需要并行节点结束时使用,达到线程协作。