Jar { public boolean takeBean() { boolean success = false; try { lock.acquire(); success = beans > 0; if (success) { beans = beans - 1; } } catch (Exception e) { e.printStackTrace(); } finally { lock.release(); } return success; } Jar(int beans, Lock lock); boolean takeBean();  }
@Test public void testTakeBeans() { Mutex mutex = new Mutex(); Jar jar = new Jar(10, mutex); for (int i = 0; i < 10; i++) { assertTrue(jar.takeBean()); } assertFalse(jar.takeBean()); }