CacheActionDispatcher implements Runnable { boolean add(CacheAction action) { int m = action.getMemory(); synchronized (monitor) { if (memory + m <= maxMemory && queue.offer(action)) { memory += m; return true; } } return false; } CacheActionDispatcher();   CacheActionDispatcher(long maxMemory); @Override void run(); void stop();  }
@Test public void testMaxQueueSize() { CacheActionDispatcher dispatcher = new CacheActionDispatcher(); for (int i = 0; i < MAX_SIZE + 10; i++) { dispatcher.add(createWriteAction(valueOf(i))); } assertEquals(MAX_SIZE, dispatcher.queue.size()); assertEquals("0", dispatcher.queue.peek().toString()); }