MonitorExample { public void demoEnterWhen() throws InterruptedException { monitor.enterWhen(conditionGuard); try { taskDoneCounter++; if (taskDoneCounter == stopTaskCount) { condition = false; } } finally { monitor.leave(); } }  void demoTryEnterIf(); void demoEnterIf(); void demoEnterWhen(); int getStopTaskCount(); void setStopTaskCount(int stopTaskCount); void setCondition(boolean condition); int getTaskSkippedCounter(); int getTaskDoneCounter();  }
@Test public void testDemoEnterWhen() throws Exception { monitorExample.setStopTaskCount(Integer.MAX_VALUE); monitorExample.setCondition(false); setUpThreadsForTestingMethod("demoEnterWhen"); startAllThreadsForTest(); int expectedCompletedCount = 0; int completedCount = monitorExample.getTaskDoneCounter(); assertThat(completedCount, is(expectedCompletedCount)); monitorExample.setCondition(true); waitForTestThreadsToFinish(); expectedCompletedCount = 10; completedCount = monitorExample.getTaskDoneCounter(); assertThat(completedCount, is(expectedCompletedCount)); }