zoukankan      html  css  js  c++  java
  • CountDownLatch、CyclicBarrier及Semaphore的用法示例

    一、参考blog

    https://www.cnblogs.com/dolphin0520/p/3920397.html

    二、CountDownLatch

    个人把它类比于一个持有计数的闸门,每到达这个闸门一个线程,计数减1,当计数为0时再执行闸门后续的动作。同时闸门失效了(只能用一次)。

    public static void main(String[] args) {
            Executor fix = Executors.newFixedThreadPool(5);
            CountDownLatch latch = new CountDownLatch(2);
            Runnable runner = () -> {
                System.out.println("[" + Thread.currentThread().getName() + "]" + " started");
                System.out.println("[" + Thread.currentThread().getName() + "]" + " finished");
                latch.countDown();
            };
            fix.execute(runner);
            fix.execute(runner);
            try {
                System.out.println("[" + Thread.currentThread().getName() + "]" + " waiting on latch...");
                latch.await();
                System.out.println("[" + Thread.currentThread().getName() + "]" + " ok, i can do my work");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

     三、CyclicBarrier

    用法差不多,特点:可以重复使用。

        public static void main(String[] args) {
            Executor fix = Executors.newFixedThreadPool(5);
            CyclicBarrier barrier = new CyclicBarrier(2);
            Runnable runner = () -> {
                try {
                    Thread.sleep(new Random().nextInt(10000));
                    System.out.println("[" + Thread.currentThread().getName() + "]" + " wait on barri " + new Date());
                    barrier.await();
                    System.out.println("[" + Thread.currentThread().getName() + "]" + " condition ok, do my work " + System.currentTimeMillis());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            };
            fix.execute(runner);
            fix.execute(runner);
        }

    四、Semaphore

     向量,有点像锁。

    import java.util.Date;
    import java.util.Random;
    import java.util.concurrent.*;
    
    public class Test {
        //    
        public static void main(String[] args) {
            Executor fix = Executors.newFixedThreadPool(5);
            Semaphore semaphore = new Semaphore(2);
    
            Runnable runner = () -> {
                try {
                    semaphore.acquire();
                    System.out.println("[" + Thread.currentThread().getName() + "]" + " get a semaphore " + new Date());
                    Thread.sleep(2000);
                    System.out.println("[" + Thread.currentThread().getName() + "]" + " release a semaphore " + new Date());
                    semaphore.release();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            };
    
            for (int i = 0; i < 5; i++) {
                fix.execute(runner);
            }
    
        }
  • 相关阅读:
    layui flow loading占位图实现方法
    layui弹出层layer的area过大被遮挡
    layui 复选框checkbox 实现全选全选
    axios 设置headers token
    elementUI vue this.$confirm 和el-dialog 弹出框 移动
    vue + axios + formdata 上传文件带参数的爬坑之路
    Java四舍五入时保留指定小数位数
    List containsKey 和Map contains 判断集合中是否包含某个值
    BigDecimal 基本使用 比较大小和加减乘除
    springMVC返回json数据乱码问
  • 原文地址:https://www.cnblogs.com/yoyotl/p/10084337.html
Copyright © 2011-2022 走看看