并发控制类:CountDownLatch、CyclicBarrier、Semaphore

CountDownLatch

  • CountDownLatch是一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待。
private static final CountDownLatch rongYao = new CountDownLatch(5);
static class Hero implements Runnable {
    @Override
    public void run() {
        try {
            Thread.sleep(new Random().nextInt(10) * 1000);
            System.out.println("--> 英雄:" + Thread.currentThread().getName() + " 准备开始战斗.....");
            rongYao.countDown();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

private static void testCountDownLatch() throws InterruptedException {
    // CountDownLatch测试
    Thread thread1 = new Thread(new Hero(), "鲁班七号");
    Thread thread2 = new Thread(new Hero(), "程咬金");
    Thread thread3 = new Thread(new Hero(), "黄忠");
    Thread thread4 = new Thread(new Hero(), "凯");
    Thread thread5 = new Thread(new Hero(), "嬴政");

    System.out.println("--> 所有英雄准备开始战斗....");
    System.out.println();

    thread1.start();
    thread2.start();
    thread3.start();
    thread4.start();
    thread5.start();

    rongYao.await();
    System.out.println("--> 所有英雄都准备好了,开撸吧...");
    System.out.println();
}
  • CyclicBarrier是一个同步辅助类,允许一组线程互相等待,直到到达某个公共屏障点 (common barrier point)。因为该 barrier 在释放等待线程后可以重用,所以称它为循环 的 barrier。
private static final CyclicBarrier cyclicBarrier = new CyclicBarrier(5, () -> System.out.println(">>>>>>>>>>>>>>>>>> 一个阶段完成 <<<<<<<<<<<<<<<<<"));
private static final CountDownLatch cr = new CountDownLatch(5);

static class Hero1 implements Runnable {
    @Override
    public void run() {
        try {
            Thread.sleep(new Random().nextInt(5) * 1000);
            System.out.println("--> 英雄:" + Thread.currentThread().getName() + " 已经到达中路,请求出击.....");
            cyclicBarrier.await();
            Thread.sleep(new Random().nextInt(10) * 1000);
            System.out.println("--> 英雄:" + Thread.currentThread().getName() + " 战胜了对手.....");
            cyclicBarrier.await();
            Thread.sleep(new Random().nextInt(3) * 1000);
            System.out.println("--> 英雄:" + Thread.currentThread().getName() + " 已经回城.....");
            cr.countDown();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
    }
}

private static void testCyclicBarrier() throws InterruptedException {
    // CountDownLatch测试
    Thread thread1 = new Thread(new Hero1(), "鲁班七号");
    Thread thread2 = new Thread(new Hero1(), "程咬金");
    Thread thread3 = new Thread(new Hero1(), "黄忠");
    Thread thread4 = new Thread(new Hero1(), "凯");
    Thread thread5 = new Thread(new Hero1(), "嬴政");

    System.out.println("--> 中路战斗准备开始....");
    System.out.println();

    thread1.start();
    thread2.start();
    thread3.start();
    thread4.start();
    thread5.start();

    cr.await();
    System.out.println("--> 中路战斗结束...");
}
  • Semaphore是一个计数信号量,它的本质是一个"[共享锁]"。
    信号量维护了一个信号量许可集。线程可以通过调用acquire()来获取信号量的许可;当信号量中有可用的许可时,线程能获取该许可;否则线程必须等待,直到有可用的许可为止。 线程可以通过release()来释放它所持有的信号量许可。
private static final Semaphore semaphore = new Semaphore(5);

private static void testSemaphore() {
    for (int i = 0; i < 10; i ++) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    if (semaphore.tryAcquire(1, new Random().nextInt(10), TimeUnit.SECONDS)) {
                        Thread.sleep(new Random().nextInt(10) * 1000);
                        System.out.println(Thread.currentThread().getName() + " 获取到了执行的机会,已经完成了要做的工作...");
                    } else {
                        System.out.println(Thread.currentThread().getName() + " 在等待了一会之后没有获取到CPU资源,退出了...");
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    semaphore.release();
                }
            }
        }, "thread--" + i).start();
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容