ResourceGuard implements AutoCloseable, Serializable { @Override public void close() { synchronized (lock) { closed = true; while (leaseCount > 0) { try { lock.wait(); } catch (InterruptedException ignore) { } } } } ResourceGuard(); Lease acquireResource(); @Override void close(); boolean isClosed(); int getLeaseCount();  }
@Test public void testClose() { ResourceGuard resourceGuard = new ResourceGuard(); Assert.assertFalse(resourceGuard.isClosed()); resourceGuard.close(); Assert.assertTrue(resourceGuard.isClosed()); try { resourceGuard.acquireResource(); Assert.fail(); } catch (IOException ignore) { } }