java中cyclicBarrier、countDownLatch、semaphore的使用

countDownLatch

在这里插入图片描述
创建countDownLatch对象
在这里插入图片描述

每次线程调用countDown
在这里插入图片描述
在主线程设置await
每次调用countDown时,countDown对象计数器都会-1,当减到3时,await就会进行释放,主线程将会启动

import java.util.concurrent.*;

public class threadpooltest{
    public static void main(String[] args) {
//        CyclicBarrier cyclicBarrier = new CyclicBarrier(3);
        CountDownLatch countDownLatch = new CountDownLatch(3);
        //创建线程池
        ExecutorService executor = new ThreadPoolExecutor(5, 5, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<>(1)) {
            @Override protected void beforeExecute(Thread t, Runnable r) {
                System.out.println("beforeExecute is called");
            }
            @Override protected void afterExecute(Runnable r, Throwable t) {
                System.out.println("afterExecute is called");
            }
            @Override protected void terminated() {
                System.out.println("terminated is called");
            }
        };
        executor.submit(()->{
            System.out.println("减一");
            try {
                countDownLatch.countDown();
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        executor.submit(()->{
            System.out.println("减一");
            try {
                countDownLatch.countDown();
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        executor.submit(()->{
            System.out.println("减一");
            try {
                countDownLatch.countDown();
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        try {
            countDownLatch.await();
            System.out.println("主线程启动");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}


在这里插入图片描述

cyclicBarrier

cyclicBarrier的作用是在所有线程都准备完成后进行同一的运行
在线程准备完成后,统一运行
在这里插入图片描述
首先创建cyclicBarrier以及计数量3
每个线程准备完成后执行await
在这里插入图片描述
当三个线程都进行await操作后,三个线程将会同时往下执行
在这里插入图片描述

import java.util.concurrent.*;

public class threadpooltest{
    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(3);
//        CountDownLatch countDownLatch = new CountDownLatch(3);
        //创建线程池
        ExecutorService executor = new ThreadPoolExecutor(5, 5, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<>(1)) {
            @Override protected void beforeExecute(Thread t, Runnable r) {
                System.out.println("beforeExecute is called");
            }
            @Override protected void afterExecute(Runnable r, Throwable t) {
                System.out.println("afterExecute is called");
            }
            @Override protected void terminated() {
                System.out.println("terminated is called");
            }
        };
        executor.submit(()->{
            System.out.println("进入准备");
            try {
                cyclicBarrier.await();
                System.out.println("同时启动");
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        executor.submit(()->{
            System.out.println("进入准备");
            try {
                cyclicBarrier.await();
                System.out.println("同时启动");
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        executor.submit(()->{
            System.out.println("进入准备");
            try {
                cyclicBarrier.await();
                System.out.println("同时启动");
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

    }
}

semaphore

semaphore是一组信号量,线程在使用acquire后会使semaphore信号量-1,若信号量为0,则线程阻塞。使用release释放信号量,semaphore信号量+1,此时若阻塞的线程获取到信号量则可继续执行

import java.util.concurrent.Semaphore;

public class SemaphoreTest {
    public static void main(String[] args) {
        //创建信号量为6的Semaphore对象
        Semaphore semaphore = new Semaphore(6);
        //创建50个线程获取信号量
        for (int i = 0; i < 50; i++) {
            new Thread(()->{
                try {
                    //获取信号量,若信号量为0则阻塞
                    semaphore.acquire();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    //释放获取的信号量
                    semaphore.release();
                }
                System.out.println("aaa");
            }).start();
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值