本文共 678 字,大约阅读时间需要 2 分钟。
interface Limiter { boolean canPass(); void exit(); } class FlowLimiter implements Limiter { private AtomicInteger atomic; private int threadMax;//通过线程的最大数量 public FlowLimiter(int threadMax){ this.threadMax = Math.abs(threadMax); this.atomic = new AtomicInteger(); } @Override public boolean canPass(){ boolean res = false; int num = atomic.getAndIncrement(); if(num <= threadMax - 1){ res = true; } return res; } @Override public void exit() { atomic.getAndDecrement(); } }
转载地址:http://iiztb.baihongyu.com/