@@ -0,0 +1,1376 @@
+package me.ehlxr;%0A%0Aimport com.google.common.collect.Lists;%0A%0Aimport java.util.Collections;%0Aimport java.util.List;%0Aimport java.util.concurrent.atomic.AtomicInteger;%0A%0A/**%0A * %E6%8C%89%E6%AF%94%E4%BE%8B%E6%8E%A7%E5%88%B6%E6%B5%81%E9%87%8F%0A *%0A * @author lixiangrong%0A * @since 2019-07-18.%0A */%0Apublic class BarrierTest %7B%0A    public static void main(String%5B%5D args) %7B%0A        Barrier barrier = new Barrier(10, 3);%0A%0A        final Thread%5B%5D threads = new Thread%5B20%5D;%0A        for (int i = 0; i %3C threads.length; i++) %7B%0A            threads%5Bi%5D = new Thread(() -%3E %7B%0A                if (barrier.allow()) %7B%0A                    System.out.println(%22yes%22);%0A                %7D%0A            %7D);%0A            threads%5Bi%5D.start();%0A        %7D%0A%0A        for (Thread t : threads) %7B%0A            try %7B%0A                t.join();%0A            %7D catch (InterruptedException e) %7B%0A                e.printStackTrace();%0A            %7D%0A        %7D%0A%0A    %7D%0A%7D%0A%0Aclass Barrier %7B%0A    private AtomicInteger op = new AtomicInteger(0);%0A    private List%3CInteger%3E sources = Lists.newArrayList();%0A    private int base;%0A    private int rate;%0A%0A    public boolean allow() %7B%0A        return sources.get(op.incrementAndGet() %25 base) %3C rate;%0A    %7D%0A%0A    private Barrier() %7B%0A    %7D%0A%0A    public Barrier(int base, int rate) %7B%0A        this.base = base;%0A        this.rate = rate;%0A%0A        for (int i = 0; i %3C 10; i++) %7B%0A            sources.add(i);%0A        %7D%0A        Collections.shuffle(sources);%0A    %7D%0A%0A%7D%0A%0A

update at 2019-07-18 18:46:11 by ehlxr