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();
}
}