Examples of ReentrantLock


Examples of java.util.concurrent.locks.ReentrantLock

   *
   * @param fair whether this monitor should use a fair ordering policy rather than a non-fair (but
   *        fast) one
   */
  public Monitor(boolean fair) {
    this.lock = new ReentrantLock(fair);
  }
View Full Code Here

Examples of java.util.concurrent.locks.ReentrantLock

   * Enters this monitor. Blocks at most the given time.
   *
   * @return whether the monitor was entered
   */
  public boolean enter(long time, TimeUnit unit) {
    final ReentrantLock lock = this.lock;
    long startNanos = System.nanoTime();
    long timeoutNanos = unit.toNanos(time);
    long remainingNanos = timeoutNanos;
    boolean interruptIgnored = false;
    try {
      while (true) {
        try {
          return lock.tryLock(remainingNanos, TimeUnit.NANOSECONDS);
        } catch (InterruptedException ignored) {
          interruptIgnored = true;
          remainingNanos = (timeoutNanos - (System.nanoTime() - startNanos));
        }
      }
View Full Code Here

Examples of java.util.concurrent.locks.ReentrantLock

   */
  public void enterWhen(Guard guard) throws InterruptedException {
    if (guard.monitor != this) {
      throw new IllegalMonitorStateException();
    }
    final ReentrantLock lock = this.lock;
    boolean reentrant = lock.isHeldByCurrentThread();
    lock.lockInterruptibly();
    try {
      waitInterruptibly(guard, reentrant);
    } catch (Throwable throwable) {
      lock.unlock();
      throw Throwables.propagate(throwable);
    }
  }
View Full Code Here

Examples of java.util.concurrent.locks.ReentrantLock

   */
  public void enterWhenUninterruptibly(Guard guard) {
    if (guard.monitor != this) {
      throw new IllegalMonitorStateException();
    }
    final ReentrantLock lock = this.lock;
    boolean reentrant = lock.isHeldByCurrentThread();
    lock.lock();
    try {
      waitUninterruptibly(guard, reentrant);
    } catch (Throwable throwable) {
      lock.unlock();
      throw Throwables.propagate(throwable);
    }
  }
View Full Code Here

Examples of java.util.concurrent.locks.ReentrantLock

   */
  public boolean enterWhen(Guard guard, long time, TimeUnit unit) throws InterruptedException {
    if (guard.monitor != this) {
      throw new IllegalMonitorStateException();
    }
    final ReentrantLock lock = this.lock;
    boolean reentrant = lock.isHeldByCurrentThread();
    long startNanos = System.nanoTime();
    if (!lock.tryLock(time, unit)) {
      return false;
    }
    boolean satisfied;
    try {
      long remainingNanos = unit.toNanos(time) - (System.nanoTime() - startNanos);
      satisfied = waitInterruptibly(guard, remainingNanos, reentrant);
    } catch (Throwable throwable) {
      lock.unlock();
      throw Throwables.propagate(throwable);
    }
    if (satisfied) {
      return true;
    } else {
      lock.unlock();
      return false;
    }
  }
View Full Code Here

Examples of java.util.concurrent.locks.ReentrantLock

   */
  public boolean enterWhenUninterruptibly(Guard guard, long time, TimeUnit unit) {
    if (guard.monitor != this) {
      throw new IllegalMonitorStateException();
    }
    final ReentrantLock lock = this.lock;
    boolean reentrant = lock.isHeldByCurrentThread();
    long startNanos = System.nanoTime();
    long timeoutNanos = unit.toNanos(time);
    long remainingNanos = timeoutNanos;
    boolean interruptIgnored = false;
    try {
      while (true) {
        try {
          if (lock.tryLock(remainingNanos, TimeUnit.NANOSECONDS)) {
            break;
          } else {
            return false;
          }
        } catch (InterruptedException ignored) {
          interruptIgnored = true;
        } finally {
          remainingNanos = (timeoutNanos - (System.nanoTime() - startNanos));
        }
      }
      boolean satisfied;
      try {
        satisfied = waitUninterruptibly(guard, remainingNanos, reentrant);
      } catch (Throwable throwable) {
        lock.unlock();
        throw Throwables.propagate(throwable);
      }
      if (satisfied) {
        return true;
      } else {
        lock.unlock();
        return false;
      }
    } finally {
      if (interruptIgnored) {
        Thread.currentThread().interrupt();
View Full Code Here

Examples of java.util.concurrent.locks.ReentrantLock

   */
  public boolean enterIf(Guard guard) {
    if (guard.monitor != this) {
      throw new IllegalMonitorStateException();
    }
    final ReentrantLock lock = this.lock;
    lock.lock();
    boolean satisfied;
    try {
      satisfied = guard.isSatisfied();
    } catch (Throwable throwable) {
      lock.unlock();
      throw Throwables.propagate(throwable);
    }
    if (satisfied) {
      return true;
    } else {
      lock.unlock();
      return false;
    }
  }
View Full Code Here

Examples of java.util.concurrent.locks.ReentrantLock

   */
  public boolean enterIfInterruptibly(Guard guard) throws InterruptedException {
    if (guard.monitor != this) {
      throw new IllegalMonitorStateException();
    }
    final ReentrantLock lock = this.lock;
    lock.lockInterruptibly();
    boolean satisfied;
    try {
      satisfied = guard.isSatisfied();
    } catch (Throwable throwable) {
      lock.unlock();
      throw Throwables.propagate(throwable);
    }
    if (satisfied) {
      return true;
    } else {
      lock.unlock();
      return false;
    }
  }
View Full Code Here

Examples of java.util.concurrent.locks.ReentrantLock

   */
  public boolean enterIf(Guard guard, long time, TimeUnit unit) {
    if (guard.monitor != this) {
      throw new IllegalMonitorStateException();
    }
    final ReentrantLock lock = this.lock;
    if (!enter(time, unit)) {
      return false;
    }
    boolean satisfied;
    try {
      satisfied = guard.isSatisfied();
    } catch (Throwable throwable) {
      lock.unlock();
      throw Throwables.propagate(throwable);
    }
    if (satisfied) {
      return true;
    } else {
      lock.unlock();
      return false;
    }
  }
View Full Code Here

Examples of java.util.concurrent.locks.ReentrantLock

  public boolean enterIfInterruptibly(Guard guard, long time, TimeUnit unit)
      throws InterruptedException {
    if (guard.monitor != this) {
      throw new IllegalMonitorStateException();
    }
    final ReentrantLock lock = this.lock;
    if (!lock.tryLock(time, unit)) {
      return false;
    }
    boolean satisfied;
    try {
      satisfied = guard.isSatisfied();
    } catch (Throwable throwable) {
      lock.unlock();
      throw Throwables.propagate(throwable);
    }
    if (satisfied) {
      return true;
    } else {
      lock.unlock();
      return false;
    }
  }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.