コード例 #1
0
 public void lockInterruptibly() throws InterruptedException {
   if (Thread.interrupted()) throw new InterruptedException();
   Thread caller = Thread.currentThread();
   synchronized (this) {
     if (owner_ == null) {
       owner_ = caller;
       holds_ = 1;
       return;
     } else if (caller == owner_) {
       incHolds();
       return;
     } else {
       try {
         do {
           wait();
         } while (owner_ != null);
         owner_ = caller;
         holds_ = 1;
         return;
       } catch (InterruptedException ex) {
         if (owner_ == null) notify();
         throw ex;
       }
     }
   }
 }
コード例 #2
0
 public void lock() {
   Thread caller = Thread.currentThread();
   synchronized (this) {
     if (owner_ == null) {
       owner_ = caller;
       holds_ = 1;
       return;
     } else if (caller == owner_) {
       incHolds();
       return;
     } else {
       boolean wasInterrupted = Thread.interrupted();
       try {
         while (true) {
           try {
             wait();
           } catch (InterruptedException e) {
             wasInterrupted = true;
             // no need to notify; if we were signalled, we
             // will act as signalled, ignoring the
             // interruption
           }
           if (owner_ == null) {
             owner_ = caller;
             holds_ = 1;
             return;
           }
         }
       } finally {
         if (wasInterrupted) Thread.currentThread().interrupt();
       }
     }
   }
 }
コード例 #3
0
 public boolean tryLock(long nanos) throws InterruptedException {
   if (Thread.interrupted()) throw new InterruptedException();
   Thread caller = Thread.currentThread();
   synchronized (this) {
     if (owner_ == null) {
       owner_ = caller;
       holds_ = 1;
       return true;
     } else if (caller == owner_) {
       incHolds();
       return true;
     }
   }
   WaitQueue.WaitNode n = new WaitQueue.WaitNode();
   return n.doTimedWait(this, nanos);
 }
コード例 #4
0
 public void lockInterruptibly() throws InterruptedException {
   if (Thread.interrupted()) throw new InterruptedException();
   Thread caller = Thread.currentThread();
   synchronized (this) {
     if (owner_ == null) {
       owner_ = caller;
       holds_ = 1;
       return;
     } else if (caller == owner_) {
       incHolds();
       return;
     }
   }
   WaitQueue.WaitNode n = new WaitQueue.WaitNode();
   n.doWait(this);
 }
コード例 #5
0
    public boolean tryLock(long nanos) throws InterruptedException {
      if (Thread.interrupted()) throw new InterruptedException();
      Thread caller = Thread.currentThread();

      synchronized (this) {
        if (owner_ == null) {
          owner_ = caller;
          holds_ = 1;
          return true;
        } else if (caller == owner_) {
          incHolds();
          return true;
        } else if (nanos <= 0) return false;
        else {
          long deadline = Utils.nanoTime() + nanos;
          try {
            for (; ; ) {
              TimeUnit.NANOSECONDS.timedWait(this, nanos);
              if (caller == owner_) {
                incHolds();
                return true;
              } else if (owner_ == null) {
                owner_ = caller;
                holds_ = 1;
                return true;
              } else {
                nanos = deadline - Utils.nanoTime();
                if (nanos <= 0) return false;
              }
            }
          } catch (InterruptedException ex) {
            if (owner_ == null) notify();
            throw ex;
          }
        }
      }
    }