### Question:
MemoryUsage extends Usage<MemoryUsage> { public void increaseUsage(long value) { if (value == 0) { return; } usageLock.writeLock().lock(); try { usage += value; setPercentUsage(caclPercentUsage()); } finally { usageLock.writeLock().unlock(); } if (parent != null) { parent.increaseUsage(value); } } MemoryUsage();  MemoryUsage(MemoryUsage parent);  MemoryUsage(String name);  MemoryUsage(MemoryUsage parent, String name);  MemoryUsage(MemoryUsage parent, String name, float portion); @Override void waitForSpace(); @Override boolean waitForSpace(long timeout); @Override boolean isFull(); void enqueueUsage(long value); void increaseUsage(long value); void decreaseUsage(long value); @Override long getUsage(); void setUsage(long usage); void setPercentOfJvmHeap(int percentOfJvmHeap);  }### Answer:
@Test public final void testPercentUsageNeedsNoThread() { int activeThreadCount = Thread.activeCount(); underTest.setLimit(10); underTest.start(); underTest.increaseUsage(1); assertEquals("usage is correct", 10, underTest.getPercentUsage()); assertEquals("no new thread created without listener or callback",activeThreadCount, Thread.activeCount()); }
@Test public final void testAddUsageListenerStartsThread() throws Exception { int activeThreadCount = Thread.activeCount(); underTest = new MemoryUsage(); underTest.setExecutor(executor); underTest.setLimit(10); underTest.start(); final CountDownLatch called = new CountDownLatch(1); final String[] listnerThreadNameHolder = new String[1]; underTest.addUsageListener(new UsageListener() { public void onUsageChanged(Usage usage, int oldPercentUsage, int newPercentUsage) { called.countDown(); listnerThreadNameHolder[0] = Thread.currentThread().toString(); } }); underTest.increaseUsage(1); assertTrue("listener was called", called.await(30, TimeUnit.SECONDS)); assertTrue("listener called from another thread", !Thread.currentThread().toString().equals(listnerThreadNameHolder[0])); assertEquals("usage is correct", 10, underTest.getPercentUsage()); assertEquals("new thread created with listener", activeThreadCount + 1, Thread.activeCount()); }