From 52178b4ff12166edb57b12975e1590dc3523509d Mon Sep 17 00:00:00 2001 From: ehlxr Date: Fri, 19 Jul 2019 14:14:12 +0800 Subject: [PATCH] update at 2019-07-19 14:14:12 by ehlxr --- src/main/java/me/ehlxr/BarrierTest.java | 64 -------------------- src/main/java/me/ehlxr/rate/Main.java | 57 +++++++++++++++++ src/main/java/me/ehlxr/rate/RateBarrier.java | 40 ++++++++++++ 3 files changed, 97 insertions(+), 64 deletions(-) delete mode 100644 src/main/java/me/ehlxr/BarrierTest.java create mode 100644 src/main/java/me/ehlxr/rate/Main.java create mode 100644 src/main/java/me/ehlxr/rate/RateBarrier.java diff --git a/src/main/java/me/ehlxr/BarrierTest.java b/src/main/java/me/ehlxr/BarrierTest.java deleted file mode 100644 index 0869e09..0000000 --- a/src/main/java/me/ehlxr/BarrierTest.java +++ /dev/null @@ -1,64 +0,0 @@ -package me.ehlxr; - -import com.google.common.collect.Lists; - -import java.util.Collections; -import java.util.List; -import java.util.concurrent.atomic.AtomicInteger; - -/** - * 按比例控制流量 - * - * @author lixiangrong - * @since 2019-07-18. - */ -public class BarrierTest { - public static void main(String[] args) { - Barrier barrier = new Barrier(10, 3); - - final Thread[] threads = new Thread[20]; - for (int i = 0; i < threads.length; i++) { - threads[i] = new Thread(() -> { - if (barrier.allow()) { - System.out.println("yes"); - } - }); - threads[i].start(); - } - - for (Thread t : threads) { - try { - t.join(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - - } -} - -class Barrier { - private AtomicInteger op = new AtomicInteger(0); - private List sources = Lists.newArrayList(); - private int base; - private int rate; - - public boolean allow() { - return sources.get(op.incrementAndGet() % base) < rate; - } - - private Barrier() { - } - - public Barrier(int base, int rate) { - this.base = base; - this.rate = rate; - - for (int i = 0; i < 10; i++) { - sources.add(i); - } - Collections.shuffle(sources); - } - -} - diff --git a/src/main/java/me/ehlxr/rate/Main.java b/src/main/java/me/ehlxr/rate/Main.java new file mode 100644 index 0000000..7982048 --- /dev/null +++ b/src/main/java/me/ehlxr/rate/Main.java @@ -0,0 +1,57 @@ +package me.ehlxr.rate; + +/** + * @author lixiangrong + * @since 2019-07-18. + */ +public class Main { + public static void main(String[] args) { + RateBarrier rateBarrier = new RateBarrier(10, 3); + + final Thread[] threads = new Thread[20]; + for (int i = 0; i < threads.length; i++) { + threads[i] = new Thread(() -> { + if (rateBarrier.allow()) { + System.out.println("this is on 3"); + } else { + System.out.println("this is on 7"); + } + }); + threads[i].start(); + } + + for (Thread t : threads) { + try { + t.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + } +} + +// Output: + +/* +this is on 7 +this is on 7 +this is on 7 +this is on 7 +this is on 3 +this is on 7 +this is on 3 +this is on 7 +this is on 7 +this is on 3 +this is on 7 +this is on 7 +this is on 7 +this is on 7 +this is on 3 +this is on 7 +this is on 3 +this is on 7 +this is on 7 +this is on 3 +*/ diff --git a/src/main/java/me/ehlxr/rate/RateBarrier.java b/src/main/java/me/ehlxr/rate/RateBarrier.java new file mode 100644 index 0000000..4aeebc0 --- /dev/null +++ b/src/main/java/me/ehlxr/rate/RateBarrier.java @@ -0,0 +1,40 @@ +package me.ehlxr.rate; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * 按比例控制流量 + * + * @author lixiangrong + * @since 2019-07-19. + */ +public class RateBarrier { + private AtomicInteger op = new AtomicInteger(0); + private List source; + private int base; + private int rate; + + public boolean allow() { + return source.get(op.incrementAndGet() % base) < rate; + } + + private RateBarrier() { + } + + public RateBarrier(int base, int rate) { + this.base = base; + this.rate = rate; + + source = new ArrayList<>(base); + for (int i = 0; i < base; i++) { + source.add(i); + } + + // 打乱集合顺序 + Collections.shuffle(source); + } + +}