測試代碼:java
package com.steel.gc; import java.util.ArrayDeque; import java.util.Deque; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import org.apache.log4j.Logger; import org.apache.log4j.spi.LoggerFactory; public class Produce implements Runnable { private static ScheduledExecutorService executorService = Executors.newScheduledThreadPool(2); private Deque<byte[]> deque; private int objectSize; private int queueSize; public Produce(int objectSize, int ttl) { this.deque = new ArrayDeque<byte[]>(); this.objectSize = objectSize; this.queueSize = ttl * 1000; } public void run() { for (int i = 0; i < 100; i++) { deque.add(new byte[objectSize]); if (deque.size() > queueSize) { deque.poll(); } } } public static void main(String[] args) throws InterruptedException { executorService.scheduleAtFixedRate(new Produce(200 * 1024 * 1024 / 1000, 5), 0, 100, TimeUnit.MILLISECONDS); executorService.scheduleAtFixedRate(new Produce(50 * 1024 * 1024 / 1000, 120), 0, 100, TimeUnit.MILLISECONDS); TimeUnit.MINUTES.sleep(5); executorService.shutdownNow(); } }
方案一:apache
方案二:測試
方案三:this
並且該策略還沒有測試完成,報內存不足,出錯3d