public int setClosed() throws ClosedChannelException {
    while (true) {
      int curBits = status.get();
      if ((curBits & STATUS_CLOSED_MASK) != 0) {
        throw new ClosedChannelException();
      }
      if (status.compareAndSet(curBits, curBits | STATUS_CLOSED_MASK)) {
        return curBits & (~STATUS_CLOSED_MASK);
      }
    }
  }
@Test
  public void testSetClosed() throws ClosedChannelException {
    CloseableReferenceCount clr = new CloseableReferenceCount();
    assertTrue("Reference count should be open", clr.isOpen());
    clr.setClosed();
    assertFalse("Reference count should be closed", clr.isOpen());
  }