滑动窗口计数java实现
滑动窗口计数有很多使用场景,比如说限流防止系统雪崩。相比计数实现,滑动窗口实现会更加平滑,能自动消除毛刺。
概念上可以参考TCP的滑窗算法,可以看一下这篇文章(http://go12345.iteye.com/blog/1744728)。在实现上,滑动窗口算法需要循环队列和线程安全保障。
下面的实现有几个点
1, 支持滑窗大小运行时动态调整
2, 基于 java8 编译器
3, DEMO实现只支持一个窗口对象,如果要支持多个,需要修改 SlotBaseCounter 类
- package slidingwindow;
- import java.util.Arrays;
- import java.util.concurrent.atomic.AtomicInteger;
- /**
- * Created by admin on 2016/02/20.
- */
- public class SlotBaseCounter {
- private int slotSize;
- private AtomicInteger[] slotCounter;
- public SlotBaseCounter(int slotSize) {
- slotSize = slotSize < 1 ? 1 : slotSize;
- this.slotSize = slotSize;
- this.slotCounter = new AtomicInteger[slotSize];
- for (int i = 0; i < this.slotSize; i++) {
- slotCounter[i] = new AtomicInteger(0);
- }
- }
- public void increaseSlot(int slotSize) {
- slotCounter[slotSize].incrementAndGet();
- }
- public void wipeSlot(int slotSize) {
- slotCounter[slotSize].set(0);
- }
- public int totalCount() {
- return Arrays.stream(slotCounter).mapToInt(slotCounter -> slotCounter.get()).sum();
- }
- @Override
- public String toString() {
- return Arrays.toString(slotCounter);
- }
- }
- package slidingwindow;
- /**
- * Created by admin on 2016/02/20.
- */
- public class SlidingWindowCounter {
- private volatile SlotBaseCounter slotBaseCounter;
- private volatile int windowSize;
- private volatile int head;
- public SlidingWindowCounter(int windowSize) {
- resizeWindow(windowSize);
- }
- public synchronized void resizeWindow(int windowSize) {
- this.windowSize = windowSize;
- this.slotBaseCounter = new SlotBaseCounter(windowSize);
- this.head = 0;
- }
- public void increase() {
- slotBaseCounter.increaseSlot(head);
- }
- public int totalAndAdvance() {
- int total = totalCount();
- advance();
- return total;
- }
- public void advance() {
- int tail = (head + 1) % windowSize;
- slotBaseCounter.wipeSlot(tail);
- head = tail;
- }
- public int totalCount() {
- return slotBaseCounter.totalCount();
- }
- @Override
- public String toString() {
- return "total = " + totalCount() + " head = " + head + " >> " + slotBaseCounter;
- }
- }
- package slidingwindow;
- import java.util.concurrent.TimeUnit;
- /**
- * Created by admin on 2016/02/20.
- */
- public class Loops {
- public static void dieLoop(Runnable runnable) {
- while (true) {
- run(runnable);
- }
- }
- public static void rateLoop(Runnable runnable, int mills) {
- while (true) {
- try {
- TimeUnit.MILLISECONDS.sleep(mills);
- } catch (InterruptedException e) {
- }
- run(runnable);
- }
- }
- public static void fixLoop(Runnable runnable, int loop) {
- for (int i = 0; i < loop; i++) {
- run(runnable);
- }
- }
- private static void run(Runnable runnable) {
- try {
- runnable.run();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- package slidingwindow;
- import org.junit.Test;
- import java.io.IOException;
- import java.util.Random;
- import java.util.Scanner;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import java.util.concurrent.ScheduledExecutorService;
- import java.util.concurrent.TimeUnit;
- /**
- * Created by admin on 2016/02/20.
- */
- public class SlidingWindowCounterTest {
- private ExecutorService es = Executors.newCachedThreadPool();
- private ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor();
- @Test
- public void testNWindow() throws IOException {
- SlidingWindowCounter swc = new SlidingWindowCounter(3);
- ses.scheduleAtFixedRate(() -> {
- Loops.fixLoop(swc::increase, new Random().nextInt(10));
- }, 10, 2, TimeUnit.MILLISECONDS);
- ses.scheduleAtFixedRate(() -> {
- System.out.println(swc);
- swc.advance();
- }, 1, 1, TimeUnit.SECONDS);
- ses.scheduleAtFixedRate(() -> {
- swc.resizeWindow(new Random().nextInt(10));
- }, 1, 10, TimeUnit.SECONDS);
- System.in.read();
- }
- @Test
- public void test1Window() {
- SlidingWindowCounter swc = new SlidingWindowCounter(1);
- System.out.println(swc);
- swc.increase();
- swc.increase();
- System.out.println(swc);
- swc.advance();
- System.out.println(swc);
- swc.increase();
- swc.increase();
- System.out.println(swc);
- }
- @Test
- public void test3Window() {
- SlidingWindowCounter swc = new SlidingWindowCounter(3);
- System.out.println(swc);
- swc.increase();
- System.out.println(swc);
- swc.advance();
- System.out.println(swc);
- swc.increase();
- swc.increase();
- System.out.println(swc);
- swc.advance();
- System.out.println(swc);
- swc.increase();
- swc.increase();
- swc.increase();
- System.out.println(swc);
- swc.advance();
- System.out.println(swc);
- swc.increase();
- swc.increase();
- swc.increase();
- swc.increase();
- System.out.println(swc);
- swc.advance();
- System.out.println(swc);
- }
- }
这是部分测试结果输出:
total = 2245 head = 0 >> [2245, 0, 0, 0, 0, 0]
total = 4561 head = 1 >> [2245, 2316, 0, 0, 0, 0]
total = 6840 head = 2 >> [2245, 2316, 2279, 0, 0, 0]
total = 8994 head = 3 >> [2245, 2316, 2279, 2154, 0, 0]
total = 11219 head = 4 >> [2245, 2316, 2279, 2154, 2225, 0]
total = 13508 head = 5 >> [2245, 2316, 2279, 2154, 2225, 2289]
total = 13602 head = 0 >> [2339, 2316, 2279, 2154, 2225, 2289]
total = 13465 head = 1 >> [2339, 2179, 2279, 2154, 2225, 2289]
total = 13474 head = 2 >> [2339, 2179, 2288, 2154, 2225, 2289]
total = 13551 head = 3 >> [2339, 2179, 2288, 2231, 2225, 2289]
total = 2192 head = 0 >> [2192]
total = 2207 head = 0 >> [2207]
total = 2291 head = 0 >> [2291]
total = 2257 head = 0 >> [2257]
total = 2250 head = 0 >> [2250]
total = 2201 head = 0 >> [2201]
total = 2299 head = 0 >> [2299]
total = 2223 head = 0 >> [2223]
total = 2190 head = 0 >> [2190]
total = 2306 head = 0 >> [2306]
total = 2290 head = 0 >> [2290, 0, 0, 0, 0, 0, 0, 0, 0]
total = 4474 head = 1 >> [2290, 2184, 0, 0, 0, 0, 0, 0, 0]
total = 6632 head = 2 >> [2290, 2184, 2158, 0, 0, 0, 0, 0, 0]
total = 8744 head = 3 >> [2290, 2184, 2158, 2112, 0, 0, 0, 0, 0]
total = 11008 head = 4 >> [2290, 2184, 2158, 2112, 2264, 0, 0, 0, 0]
total = 13277 head = 5 >> [2290, 2184, 2158, 2112, 2264, 2269, 0, 0, 0]
total = 15446 head = 6 >> [2290, 2184, 2158, 2112, 2264, 2269, 2169, 0, 0]
total = 17617 head = 7 >> [2290, 2184, 2158, 2112, 2264, 2269, 2169, 2171, 0]
total = 19749 head = 8 >> [2290, 2184, 2158, 2112, 2264, 2269, 2169, 2171, 2132]
total = 19608 head = 0 >> [2149, 2184, 2158, 2112, 2264, 2269, 2169, 2171, 2132]
total = 2256 head = 0 >> [2256, 0, 0, 0]
total = 4624 head = 1 >> [2256, 2368, 0, 0]
total = 6811 head = 2 >> [2256, 2368, 2187, 0]
total = 8973 head = 3 >> [2256, 2368, 2187, 2162]
total = 8934 head = 0 >> [2217, 2368, 2187, 2162]
total = 8798 head = 1 >> [2217, 2232, 2187, 2162]
total = 8912 head = 2 >> [2217, 2232, 2301, 2162]
total = 8940 head = 3 >> [2217, 2232, 2301, 2190]
total = 8987 head = 0 >> [2264, 2232, 2301, 2190]
total = 9049 head = 1 >> [2264, 2294, 2301, 2190]
total = 2220 head = 0 >> [2220, 0, 0, 0, 0, 0, 0]
total = 4477 head = 1 >> [2220, 2257, 0, 0, 0, 0, 0]
total = 6718 head = 2 >> [2220, 2257, 2241, 0, 0, 0, 0]
total = 8939 head = 3 >> [2220, 2257, 2241, 2221, 0, 0, 0]
total = 11174 head = 4 >> [2220, 2257, 2241, 2221, 2235, 0, 0]
total = 13420 head = 5 >> [2220, 2257, 2241, 2221, 2235, 2246, 0]
total = 15673 head = 6 >> [2220, 2257, 2241, 2221, 2235, 2246, 2253]
total = 15779 head = 0 >> [2326, 2257, 2241, 2221, 2235, 2246, 2253]
total = 15796 head = 1 >> [2326, 2274, 2241, 2221, 2235, 2246, 2253]
total = 15802 head = 2 >> [2326, 2274, 2247, 2221, 2235, 2246, 2253]
滑动窗口计数java实现的更多相关文章
- Storm 实现滑动窗口计数和TopN排序
计算top N words的topology, 用于比如trending topics or trending images on Twitter. 实现了滑动窗口计数和TopN排序, 比较有意思, ...
- 【Java】 剑指offer(59-1) 滑动窗口的最大值
本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集 题目 给定一个数组和滑动窗口的大小,请找出所有滑动窗口里的最大值.例 ...
- Leetcode 239题 滑动窗口最大值(Sliding Window Maximum) Java语言求解
题目链接 https://leetcode-cn.com/problems/sliding-window-maximum/ 题目内容 给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧 ...
- Java实现 LeetCode 480 滑动窗口中位数
480. 滑动窗口中位数 中位数是有序序列最中间的那个数.如果序列的大小是偶数,则没有最中间的数:此时中位数是最中间的两个数的平均数. 例如: [2,3,4],中位数是 3 [2,3],中位数是 (2 ...
- Java实现 LeetCode 239 滑动窗口最大值
239. 滑动窗口最大值 给定一个数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧.你只可以看到在滑动窗口内的 k 个数字.滑动窗口每次只向右移动一位. 返回滑动窗口中的最 ...
- Sentinel源码分析-滑动窗口统计原理
滑动窗口技术是Sentinel比较关键的核心技术,主要用于数据统计 通过分析StatisticSlot来慢慢引出这个概念 @Override public void entry(Context con ...
- uva 1606 amphiphilic carbon molecules【把缩写写出来,有惊喜】(滑动窗口)——yhx
Shanghai Hypercomputers, the world's largest computer chip manufacturer, has invented a new classof ...
- hdu-5497 Inversion(滑动窗口+树状数组)
题目链接: Inversion Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- storm 1.0版本滑动窗口的实现及原理
滑动窗口在监控和统计应用的场景比较广泛,比如每隔一段时间(10s)统计最近30s的请求量或者异常次数,根据请求或者异常次数采取相应措施.在storm1.0版本之前,没有提供关于滑动窗口的实现,需要开发 ...
随机推荐
- 解决NVidia显卡最大化和最小化窗口时的卡顿问题
最近因为做一个3D的项目,换上了一块Nvidia的显卡.然而,在使用的过程中,发现最大化和最小化窗口时的动画有卡顿现象,并且一般出现一次后便不会再出现, 可是等一会又会出现同样问题. 我以前使用i7的 ...
- JAVA常见算法题(五)
package com.xiaowu.demo; /** * 利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示. * * * @ ...
- 利用【深度网络】高效提取feature
extracting features from a learned model, and add some new features yourself.
- Cocos2d-x游戏开发之luaproject创建
操作系统:OS X 10.85 Cocos2d-x 版本号: 2.2.1 使用Cocos2d-x 能够创建luaproject,已经使用cpp创建的project也能够继承lua进行开发,可是lua并 ...
- elasticsearch 插入数据
1.单条插入(推荐设定主键id防止重复) public static String addIndex(String index,String type,HashMap<String, Objec ...
- ARM指令中的函数调用
1. 重要寄存器 SP 栈指针, 每一种异常模式都有其自己独立的r13,它通常指向异常模式所专用的堆栈,也就是说五种异常模式.非异常模式(用户模式和系统模式),都有各自独立的堆栈,用不同的堆栈指针来 ...
- VB程序无法运行,Component ‘MCI32.OCX’错误怎么办
1 提示Component 'MCI32.OCX'错误 2 搜索你电脑的MCI32.OCX这个文件 3 把它复制到任意位置,然后再同一个目录下新建一个文本文档,输入regsvr32 MCI32 ...
- RxJava操作符总结之过滤
RxJava操作符总结之过滤 jsut() just(T t1, T t2, T t3 ....) ,just能够传入多个同样类型的參数,并将当前參数一个接着一个的发送. Observable.jus ...
- iOS学习笔记之蓝牙(有关蓝牙设备mac地址处理)
原文: http://blog.sina.com.cn/s/blog_6f2f0bed0102xn0e.html
- 怎么运行Typescript
依据官方示例: npm i -g typescript 示例:tsc *.ts 实例:tsc hello.ts 不过以上实现的太有限制了,如下实现可满足正常测试以及学习使用 package,json ...