前面了解到闭锁,我们今天深入一下,看看它的其它方面。
我们去StackOverflow上面找个问题:
Java concurrency: Countdown latch vs Cyclic barrier
java的闭锁和栅栏的区别是?
“栅栏类似于闭锁。两者的关键区别在于,所有线程必须同时到达栅栏位置,才能继续执行。闭锁用于等待事件,而栅栏用于等待其它线程。”
来看一个栅栏的例子:
import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; class Student implements Runnable { private final String name; private final CyclicBarrier barrier; public Student(String name, CyclicBarrier barrier) { this.name = name; this.barrier = barrier; } @Override public void run() { SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd HH:mm:ss"); System.out.println(format.format(Calendar.getInstance().getTime()) + " " + name + ":我去吃个饭先。"); try { Thread.sleep(2000); } catch (InterruptedException e1) { e1.printStackTrace(); } System.out.println(format.format(Calendar.getInstance().getTime()) + " " + name + ":我到华科大门了"); try { barrier.await(); } catch (InterruptedException e) { e.printStackTrace(); } catch (BrokenBarrierException e) { e.printStackTrace(); } } } public class CyclicBarrierTest { public static void main(String[] args) { int count = 2; CyclicBarrier barrier = new CyclicBarrier(count, new Runnable() { @Override public void run() { SimpleDateFormat format = new SimpleDateFormat( "yyyyMMdd HH:mm:ss"); System.out.println(format.format(Calendar.getInstance().getTime()) + " 月半小夜曲:好!既然大家都到了!我们就开吧!"); } }); Student studentA = new Student("匆匆那年", barrier); Student studentB = new Student("平凡之路", barrier); ExecutorService executorService = Executors.newFixedThreadPool(count); executorService.execute(studentA); try { //模拟B同学3秒之后才收到去玩的通知 Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } executorService.execute(studentB); executorService.shutdown(); } }某次执行结果如下:
20141112 09:28:22 匆匆那年:我去吃个饭先。 20141112 09:28:24 匆匆那年:我到华科大门了 20141112 09:28:25 平凡之路:我去吃个饭先。 20141112 09:28:27 平凡之路:我到华科大门了 20141112 09:28:27 月半小夜曲:好!既然大家都到了!我们就开吧!例子参考 Jakob Jenkov
从执行结果分析,我们假设A,B同学被班长(主线程)通知去一起去玩个游戏,B同学被通知的时间晚了3秒钟。之后A,B同学都是要先吃饭,大家吃饭都要花2秒钟。吃饭之后就到了华科大门等对方到来。大家都到了之后就开搞。OK,就是这么一个场景。我们看到在22秒的时刻,匆匆那年同学说他在吃饭,24秒的时候说他在大门了。25秒(22+3)的时候平凡之路也说他在吃饭,27秒的时候她也到华科大门了。27秒他们见面之后,月半小夜曲说既然大家都到了,那就开吧!
我们再来看另外一个闭锁的例子:
import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class CountDownLatchTester { public static void main(String[] args) { CountDownLatch latch = new CountDownLatch(3); Waiter waiter = new Waiter(latch); Feeder feeder = new Feeder(latch); ExecutorService executorService = Executors.newFixedThreadPool(2); executorService.execute(waiter); executorService.execute(feeder); executorService.shutdown(); } } class Waiter implements Runnable { CountDownLatch latch; public Waiter(CountDownLatch latch) { super(); this.latch = latch; } @Override public void run() { SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd HH:mm:ss"); try { System.out.println(format.format(Calendar.getInstance().getTime()) + " " + Thread.currentThread() + " 我饿了,先休息会。"); latch.await(); System.out.println(format.format(Calendar.getInstance().getTime()) + " " + Thread.currentThread() + " 满血满魔复活,开搞。"); } catch (InterruptedException e) { e.printStackTrace(); } } } class Feeder implements Runnable { CountDownLatch latch; public Feeder(CountDownLatch latch) { super(); this.latch = latch; } @Override public void run() { SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd HH:mm:ss"); while (latch.getCount() > 0) { // 每2秒喂一下Waiter System.out.println(format.format(Calendar.getInstance().getTime()) + " " + Thread.currentThread() + " 给Waiter喂点吃的"); latch.countDown(); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } } }某次执行的输出结果是:
20141112 10:00:03 Thread[pool-1-thread-1,5,main] 我饿了,先休息会。 20141112 10:00:03 Thread[pool-1-thread-2,5,main] 给Waiter喂点吃的 20141112 10:00:05 Thread[pool-1-thread-2,5,main] 给Waiter喂点吃的 20141112 10:00:07 Thread[pool-1-thread-2,5,main] 给Waiter喂点吃的 20141112 10:00:07 Thread[pool-1-thread-1,5,main] 满血满魔复活,开搞。例子参考 自
与上一篇中用到的闭锁的例子不同,这次是一个线程负责减少latch,另一个线程负责等待。
从栅栏和闭锁的例子看出来,一般情况下,栅栏是等待线程的总数增加到某一个值,而闭锁是等待事件减少到0。