• 模仿CyclicBarrier,自定义自己屏障类


    简介

    在这里模仿CyclicBarrier,自定义一个自己多线程屏障类,里面有个计时器count,count为0时,才唤醒线程,否则就await挂起,(没错就是用的object类的挂起和唤醒全部线程方法)

    1、MyCyclicBarrier

    package com.jacky;
    
    import java.util.concurrent.BrokenBarrierException;
    import java.util.concurrent.locks.Condition;
    import java.util.concurrent.locks.ReentrantLock;
    
    /**
     * Created by jacky on 2018/2/11.
     */
    public class MyCyclicBarrier {
        private int count;
        private int parties;
        private  Runnable barrierAction;
    
        private final ReentrantLock lock = new ReentrantLock();
    
        private final Condition condition = lock.newCondition();
    
    
        public MyCyclicBarrier(int parties,Runnable barrierAction){
            if (parties <=0){
                throw new IllegalArgumentException();
            }
            this.parties = parties;
            this.count = parties;
            this.barrierAction = barrierAction;
        }
        public  int await() throws InterruptedException,BrokenBarrierException {
            lock.lock();
            try {
                int index = --count;
                if (index ==0){
                  if (null == barrierAction){
                      barrierAction.run();
                  }
                    condition.signalAll();
                   return  index;
                }
                for (;;){
                     condition.await();
                    return index;
                }
            }finally {
                lock.unlock();
            }
        }
    }

    2、测试

    package com.jacky;
    
    import com.sun.org.apache.xpath.internal.SourceTree;
    
    import java.util.concurrent.BrokenBarrierException;
    import java.util.concurrent.CyclicBarrier;
    
    /**
     * Created by jacky on 2018/2/11.
     */
    public class CyclicBarrierDemo {
        public static void main(String[] args) throws InterruptedException {
            MyCyclicBarrier barrier = new MyCyclicBarrier(3, new Runnable() {
                @Override
                public void run() {
                    Thread thread = Thread.currentThread();
                    System.out.println("barrierAction start"+thread.getName());
                    try {
                        Thread.sleep((int)Math.random()*300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("barrierAction start"+thread.getName());
                }
            });
            Runnable runnable1 = new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep((int)(Math.random()*100));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Thread thread = Thread.currentThread();
                    System.out.println("thread start:"+thread.getName());
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    try {
                        barrier.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    }
                    System.out.println("thread end:"+thread.getName());
                }
            };
            Runnable runnable2 = new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep((int)(Math.random()*200));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Thread thread = Thread.currentThread();
                    System.out.println("thread start:"+thread.getName());
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    try {
                        barrier.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    }
                    System.out.println("thread end:"+thread.getName());
                }
            };
            Thread thread1 = new Thread(runnable1);
            Thread thread2 = new Thread(runnable2);
            Thread thread3 = new Thread(runnable1);
            thread1.start();
            thread2.start();
            thread3.start();
            Thread.sleep(2000);
            System.out.println("thread1:"+thread1.getName()+","+thread1.getState());
            System.out.println("thread2:"+thread2.getName()+","+thread2.getState());
            System.out.println("thread3:"+thread3.getName()+","+thread3.getState());
        }
    }
  • 相关阅读:
    Qt5官方demo解析集21——Extending QML
    多封装,少开放。强烈建议C++标准添加class之间的注入机制
    iOS 设计模式之工厂模式
    golang的select典型用法
    Go的异常处理 defer, panic, recover
    Visual Studio Code 的简单试用体验
    在Visual Studio Code中配置GO开发环境
    Go语言开发环境配置
    Go 语言 很牛
    Go将统治下一个10年?Go语言发展现状分析
  • 原文地址:https://www.cnblogs.com/520playboy/p/8446189.html
Copyright © 2020-2023  润新知