栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

滑动窗口Java实现

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

滑动窗口Java实现

最近在看限流相关的技术,看到几种主流的实现限流功能的思想,但刚开始看的时候对令牌桶,漏桶和滑动窗口搞懵逼了,感觉它们的思想好像都是一样的,但为什么又叫不同的名字呢?看别人的代码又看不进去,那么就根据它们的思想自己实现一遍吧
不废话,直接上代码:
public class RealSlidingWindows {

    
    private Long window;

    
    private Long totalPassCount;

    
    private Integer unitWindowCount;

    private ConcurrentlinkedDeque linkedDeque;

    private Long unitWindow;

    private AtomicLong windowPassedCount;

    private static Lock lock = new ReentrantLock();

    public static void main(String[] args) {

        long startTime = System.currentTimeMillis();

        AtomicLong passCount = new AtomicLong(0);

        AtomicLong refuseCount = new AtomicLong(0);

        RealSlidingWindows rsw = new RealSlidingWindows(100000L, 100L, 10);

        ExecutorService executorService = Executors.newFixedThreadPool(100);

        for(int i=0;i<100_000;++i){

            //单线程测试
//            if(rsw.isPass()){
//                passCount.incrementAndGet();
//            }else{
//                refuseCount.incrementAndGet();
//            }

            Runnable r = () -> {
                if(rsw.isPass()){
                    passCount.incrementAndGet();
                }else{
                    refuseCount.incrementAndGet();
                }
            };

            executorService.execute(r);
        }
        try {
            Thread.sleep(10_000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("耗时:"+(System.currentTimeMillis() - startTime)
                + " passed: "+passCount.get() +" refused: "+refuseCount.get());

        executorService.shutdown();
    }


    public RealSlidingWindows(Long window, Long totalPassCount, Integer unitWindowCount){
        this.window = window;
        this.totalPassCount = totalPassCount;
        this.unitWindowCount = unitWindowCount;
        this.linkedDeque = new ConcurrentlinkedDeque();
        this.unitWindow = window / unitWindowCount;
        this.windowPassedCount = new AtomicLong(0);
    }

    
    public  boolean isPass(){
        long now = System.currentTimeMillis();
        UnitWindows unitWindows = new UnitWindows(now);
        if(!linkedDeque.isEmpty()){
            UnitWindows first = linkedDeque.peekFirst();
            if(now - first.getStart() < unitWindow){
                //如果第一个小单元窗口还未结束
                //获取第一个单元小窗口
                unitWindows = first;
            }else{
                //第一个小窗口已经结束了
                //窗口已完成的总流量减去最后一个窗口的已完成流量即为当前窗口总流量
                windowPassedCount.addAndGet(-linkedDeque.peekLast().count.get());
                //将最后一个节点(最老的一个单元窗口)去掉
                linkedDeque.removeLast();
                //将新构建的单元窗口放到队头
                linkedDeque.addFirst(unitWindows);
            }
        } else {
            //将新构建的单元窗口放到队头
            linkedDeque.addFirst(unitWindows);
        }
        if(windowPassedCount.incrementAndGet() <= totalPassCount){
            //如果窗口已完成的流量数小于窗口允许的总流量数
            //更新窗口已完成的流量数并返回true
            unitWindows.count.incrementAndGet();
            return true;
        }
        return false;
    }

    
    public static class UnitWindows {

        public UnitWindows(Long start){
            this.start = start;
            this.count = new AtomicLong(0);
        }

        private Long start;
        private AtomicLong count;

        public Long getStart() {
            return start;
        }

        public void setStart(Long start) {
            this.start = start;
        }

        public Long getCount() {
            return count.get();
        }

        public void increment(){
            count.incrementAndGet();
        }
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/325031.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号