Java并发工具-CountDownLatch、CyclicBarrier、Semaphore

CountDownLatch等待线程完成

CountDownLatch是一个等待所有线程完成的工具,类似join()方法,初始化的时候会调用构造器传递线程个数,调用countDown方法将count值减一,调用await方法会等待知道count=0才会通过。写一个简单的例子方便理解

import java.util.concurrent.CountDownLatch;

/**
 * countDownLatch测试类
 *
 * @author wangmj
 * @since 2019/1/24
 */
public class CountDownLatchDemo {
    private static CountDownLatch countDownLatch = new CountDownLatch(2);

    public static void main(String[] args) throws InterruptedException {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("线程1开始");
                countDownLatch.countDown();
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("线程2开始");
                countDownLatch.countDown();
            }
        }).start();
        //此时会等待线程1和线程2执行完才会执行下面代码
        countDownLatch.await();
        System.out.println("3");
    }
}

Semaphore 信号量

信号量是一个可以控制同时有几个线程执行,其他线程等待的工具类,就好像一个门同时只有3个人通过,其他人则在门外等候,当三个人进门之后其他人才会下次三个进入门


import java.util.concurrent.*;

/**
 * @author wangmj
 * @since 2019/1/24
 */
public class SemaphoreDemo {

    //同时只有三个线程执行
    private static Semaphore semaphore = new Semaphore(3);

    private static ExecutorService executor = Executors.newFixedThreadPool(10);

    public static void main(String[] args) {

        long start = System.nanoTime();

        for (int i = 0; i < 10; i++) {
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    try {
                        semaphore.acquire();
                        Thread.sleep(1000);
                        System.out.println("thread name:" + Thread.currentThread().getName()+" and nanoTime:"
                                + TimeUnit.NANOSECONDS.toSeconds(System.nanoTime()-start));
                        semaphore.release();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
        executor.shutdown();
    }

}

运行结果如下

thread name:pool-1-thread-3 and nanoTime:1
thread name:pool-1-thread-2 and nanoTime:1
thread name:pool-1-thread-1 and nanoTime:1
thread name:pool-1-thread-5 and nanoTime:2
thread name:pool-1-thread-4 and nanoTime:2
thread name:pool-1-thread-6 and nanoTime:2
thread name:pool-1-thread-7 and nanoTime:3
thread name:pool-1-thread-8 and nanoTime:3
thread name:pool-1-thread-9 and nanoTime:3
thread name:pool-1-thread-10 and nanoTime:4

可以看到同一时间只允许三个线程运行,以上的原理都是基于AQS同步器实现的,可以参考我上两篇博客

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值