滑动窗口计数java实现

滑动窗口计数有很多使用场景,比如说限流防止系统雪崩。相比计数实现,滑动窗口实现会更加平滑,能自动消除毛刺。

 

概念上可以参考TCP的滑窗算法,可以看一下这篇文章(http://go12345.iteye.com/blog/1744728)。在实现上,滑动窗口算法需要循环队列和线程安全保障。

 

下面的实现有几个点

1, 支持滑窗大小运行时动态调整

2, 基于 java8 编译器

3, DEMO实现只支持一个窗口对象,如果要支持多个,需要修改 SlotBaseCounter 类 

 

 

Java代码  收藏代码
  1. package slidingwindow;  
  2.   
  3.   
  4.   
  5. import java.util.Arrays;  
  6. import java.util.concurrent.atomic.AtomicInteger;  
  7.   
  8. /** 
  9.  * Created by admin on 2016/02/20. 
  10.  */  
  11. public class SlotBaseCounter {  
  12.     private int slotSize;  
  13.     private AtomicInteger[] slotCounter;  
  14.   
  15.     public SlotBaseCounter(int slotSize) {  
  16.         slotSize = slotSize < 1 ? 1 : slotSize;  
  17.         this.slotSize = slotSize;  
  18.         this.slotCounter = new AtomicInteger[slotSize];  
  19.         for (int i = 0; i < this.slotSize; i++) {  
  20.             slotCounter[i] = new AtomicInteger(0);  
  21.         }  
  22.     }  
  23.   
  24.     public void increaseSlot(int slotSize) {  
  25.         slotCounter[slotSize].incrementAndGet();  
  26.     }  
  27.   
  28.     public void wipeSlot(int slotSize) {  
  29.         slotCounter[slotSize].set(0);  
  30.     }  
  31.   
  32.     public int totalCount() {  
  33.         return Arrays.stream(slotCounter).mapToInt(slotCounter -> slotCounter.get()).sum();  
  34.     }  
  35.   
  36.     @Override  
  37.     public String toString() {  
  38.         return Arrays.toString(slotCounter);  
  39.     }  
  40. }  

 

Java代码  收藏代码
  1. package slidingwindow;  
  2.   
  3. /** 
  4.  * Created by admin on 2016/02/20. 
  5.  */  
  6. public class SlidingWindowCounter {  
  7.     private volatile SlotBaseCounter slotBaseCounter;  
  8.     private volatile int windowSize;  
  9.     private volatile int head;  
  10.   
  11.     public SlidingWindowCounter(int windowSize) {  
  12.         resizeWindow(windowSize);  
  13.     }  
  14.   
  15.     public synchronized void resizeWindow(int windowSize) {  
  16.         this.windowSize = windowSize;  
  17.         this.slotBaseCounter = new SlotBaseCounter(windowSize);  
  18.         this.head = 0;  
  19.     }  
  20.   
  21.     public void increase() {  
  22.         slotBaseCounter.increaseSlot(head);  
  23.     }  
  24.   
  25.     public int totalAndAdvance() {  
  26.         int total = totalCount();  
  27.         advance();  
  28.         return total;  
  29.     }  
  30.   
  31.     public void advance() {  
  32.         int tail = (head + 1) % windowSize;  
  33.         slotBaseCounter.wipeSlot(tail);  
  34.         head = tail;  
  35.     }  
  36.   
  37.     public int totalCount() {  
  38.         return slotBaseCounter.totalCount();  
  39.     }  
  40.   
  41.     @Override  
  42.     public String toString() {  
  43.         return "total = " + totalCount() + " head = " + head + " >> " + slotBaseCounter;  
  44.     }  
  45. }  

 

 

Java代码  收藏代码
  1. package slidingwindow;  
  2.   
  3. import java.util.concurrent.TimeUnit;  
  4.   
  5. /** 
  6.  * Created by admin on 2016/02/20. 
  7.  */  
  8. public class Loops {  
  9.   
  10.     public static void dieLoop(Runnable runnable) {  
  11.         while (true) {  
  12.             run(runnable);  
  13.         }  
  14.     }  
  15.   
  16.     public static void rateLoop(Runnable runnable, int mills) {  
  17.         while (true) {  
  18.             try {  
  19.                 TimeUnit.MILLISECONDS.sleep(mills);  
  20.             } catch (InterruptedException e) {  
  21.   
  22.             }  
  23.             run(runnable);  
  24.         }  
  25.     }  
  26.   
  27.     public static void fixLoop(Runnable runnable, int loop) {  
  28.         for (int i = 0; i < loop; i++) {  
  29.             run(runnable);  
  30.         }  
  31.     }  
  32.   
  33.     private static void run(Runnable runnable) {  
  34.         try {  
  35.             runnable.run();  
  36.         } catch (Exception e) {  
  37.             e.printStackTrace();  
  38.         }  
  39.     }  
  40.   
  41. }  

 

 

 

Java代码  收藏代码
  1. package slidingwindow;  
  2.   
  3. import org.junit.Test;  
  4.   
  5. import java.io.IOException;  
  6. import java.util.Random;  
  7. import java.util.Scanner;  
  8. import java.util.concurrent.ExecutorService;  
  9. import java.util.concurrent.Executors;  
  10. import java.util.concurrent.ScheduledExecutorService;  
  11. import java.util.concurrent.TimeUnit;  
  12.   
  13. /** 
  14.  * Created by admin on 2016/02/20. 
  15.  */  
  16. public class SlidingWindowCounterTest {  
  17.   
  18.     private ExecutorService es = Executors.newCachedThreadPool();  
  19.     private ScheduledExecutorService ses = Executors.newSingleThreadScheduledExecutor();  
  20.   
  21.     @Test  
  22.     public void testNWindow() throws IOException {  
  23.         SlidingWindowCounter swc = new SlidingWindowCounter(3);  
  24.         ses.scheduleAtFixedRate(() -> {  
  25.             Loops.fixLoop(swc::increase, new Random().nextInt(10));  
  26.         }, 10, 2, TimeUnit.MILLISECONDS);  
  27.         ses.scheduleAtFixedRate(() -> {  
  28.             System.out.println(swc);  
  29.             swc.advance();  
  30.         }, 1, 1, TimeUnit.SECONDS);  
  31.         ses.scheduleAtFixedRate(() -> {  
  32.             swc.resizeWindow(new Random().nextInt(10));  
  33.         }, 1, 10, TimeUnit.SECONDS);  
  34.         System.in.read();  
  35.     }  
  36.   
  37.   
  38.     @Test  
  39.     public void test1Window() {  
  40.         SlidingWindowCounter swc = new SlidingWindowCounter(1);  
  41.         System.out.println(swc);  
  42.         swc.increase();  
  43.         swc.increase();  
  44.         System.out.println(swc);  
  45.         swc.advance();  
  46.         System.out.println(swc);  
  47.         swc.increase();  
  48.         swc.increase();  
  49.         System.out.println(swc);  
  50.     }  
  51.   
  52.     @Test  
  53.     public void test3Window() {  
  54.         SlidingWindowCounter swc = new SlidingWindowCounter(3);  
  55.         System.out.println(swc);  
  56.         swc.increase();  
  57.         System.out.println(swc);  
  58.         swc.advance();  
  59.         System.out.println(swc);  
  60.         swc.increase();  
  61.         swc.increase();  
  62.         System.out.println(swc);  
  63.         swc.advance();  
  64.         System.out.println(swc);  
  65.         swc.increase();  
  66.         swc.increase();  
  67.         swc.increase();  
  68.         System.out.println(swc);  
  69.         swc.advance();  
  70.         System.out.println(swc);  
  71.         swc.increase();  
  72.         swc.increase();  
  73.         swc.increase();  
  74.         swc.increase();  
  75.         System.out.println(swc);  
  76.         swc.advance();  
  77.         System.out.println(swc);  
  78.     }  
  79.   
  80. }  

 

这是部分测试结果输出:

 

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]

posted @ 2016-09-07 16:39  穿林度水  阅读(9786)  评论(0编辑  收藏  举报