コード例 #1
0
 // Release the lock by setting state to zero
 protected boolean tryRelease(int releases) {
   assert releases == 1; // Otherwise unused
   if (getState() == 0) throw new IllegalMonitorStateException();
   setExclusiveOwnerThread(null);
   setState(0);
   return true;
 }
コード例 #2
0
 // Acquire the lock if state is zero
 public boolean tryAcquire(int acquires) {
   assert acquires == 1; // Otherwise unused
   if (compareAndSetState(0, 1)) {
     setExclusiveOwnerThread(Thread.currentThread());
     return true;
   }
   return false;
 }