• 循环栅栏:CyclicBarrier(司令要求任务) 读书笔记


    可以理解为循环栅栏,栅栏就是一种障碍物.假如我们将计数器设置为10,那么凑齐第一批10个线程后,计数器就会归零,然后接着凑齐下一批10个线程,这就是循环栅栏的含义.
    构造器:
    public CyclicBarrier(int parties, Runnable barrierAction)
    parties:计数总数,也就是参与的线程总数. barrierAction 当计数器一次完成计数后,系统会执行的动作
     
    下面代码展示了 司令要求10个士兵去完成任务,先集合10个然后去一起完成任务,等全部完成后 司令才会宣布任务完成!
     
    public class CyclicBarrierDemo {
        public static class Soldier implements Runnable {
            private String soldier;
            private final CyclicBarrier cyclic;
    
            public Soldier(CyclicBarrier cyclic, String soldier) {
                this.soldier = soldier;
                this.cyclic = cyclic;
            }
    
            /**
             * When an object implementing interface <code>Runnable</code> is used
             * to create a thread, starting the thread causes the object's
             * <code>run</code> method to be called in that separately executing
             * thread.
             * <p>
             * The general contract of the method <code>run</code> is that it may
             * take any action whatsoever.
             *
             * @see Thread#run()
             */
            @Override
            public void run() {
                try {
                    //等待所有士兵到齐
                    cyclic.await();
                    doWork();
                    //等待所有士兵完成工作
                    cyclic.await();
                } catch (InterruptedException e) {//在等待过程中,线程被中断
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {//表示当前CyclicBarrier已经损坏.系统无法等到所有线程到齐了.
                    e.printStackTrace();
                }
            }
    
            void doWork() {
                try {
                    Thread.sleep(Math.abs(new Random().nextInt() % 10000));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(soldier + ":任务完成");
            }
    
        }
    
        public static class BarrierRun implements Runnable {
            boolean flag;
            int N;
    
            public BarrierRun(boolean flag, int N) {
                this.flag = flag;
                this.N = N;
            }
    
            /**
             * When an object implementing interface <code>Runnable</code> is used
             * to create a thread, starting the thread causes the object's
             * <code>run</code> method to be called in that separately executing
             * thread.
             * <p>
             * The general contract of the method <code>run</code> is that it may
             * take any action whatsoever.
             *
             * @see Thread#run()
             */
            @Override
            public void run() {
                if (flag) {
                    System.out.println("司令:[士兵" + N + "个,任务完成!]");
                } else {
                    System.out.println("司令:[士兵" + N + "个,集合完毕!]");
                    flag = true;
                }
            }
        }
    
        public static void main(String[] args) {
            final int N = 10;
            Thread[] allSoldier = new Thread[N];
            boolean flag = false;
            CyclicBarrier cyclic = new CyclicBarrier(N, new BarrierRun(flag, N));
            //设置屏障点,主要为了执行这个方法
            System.out.println("集合队伍! ");
            for (int i = 0; i < N; i++) {
                System.out.println("士兵" + i + "报道! ");
                allSoldier[i] = new Thread(new Soldier(cyclic, "士兵" + i));
                allSoldier[i].start();
            }
        }
    }
     
    结果:
  • 相关阅读:
    XSS跨站脚本攻击实例讲解,新浪微博XSS漏洞过程分析
    PHP常量PHP_SAPI与函数php_sapi_name()简介,PHP运行环境检测
    PHP导出数据到CSV文件函数/方法
    iOS8 Core Image In Swift:视频实时滤镜
    实战:mysql版本号升级
    Apache Shiro 使用手冊 链接文件夹整理
    Windows 驱动开发
    Python标准库:内置函数bytearray([source[, encoding[, errors]]])
    cocos2d-x 2.2.3 之菜单分析(1)
    JSP具体篇——out
  • 原文地址:https://www.cnblogs.com/ten951/p/6212160.html
Copyright © 2020-2023  润新知