Ali开源软件Sentinel的思考 Issue #59:关于线程限流问题的讨论

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

    }

你可能感兴趣的:(中间件)