java.util.concurrent.locks.ReentrantLock#tryLock()源码实例Demo

下面列出了java.util.concurrent.locks.ReentrantLock#tryLock() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: codebuff   文件: Monitor.java
/**
 * Enters this monitor if the guard is satisfied. Blocks at most the given time acquiring the
 * lock, but does not wait for the guard to be satisfied, and may be interrupted.
 *
 * @return whether the monitor was entered, which guarantees that the guard is now satisfied
 */


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 = false;
  try {
    return satisfied = guard.isSatisfied();
  } finally {
    if (!satisfied) {
      lock.unlock();
    }
  }
}
 
源代码2 项目: codebuff   文件: Monitor.java
/**
 * Enters this monitor if it is possible to do so immediately and the guard is satisfied. Does not
 * block acquiring the lock and does not wait for the guard to be satisfied.
 *
 * <p><b>Note:</b> This method disregards the fairness setting of this monitor.
 *
 * @return whether the monitor was entered, which guarantees that the guard is now satisfied
 */


public boolean tryEnterIf(Guard guard) {
  if (guard.monitor != this) {
    throw new IllegalMonitorStateException();
  }
  final ReentrantLock lock = this.lock;
  if (!lock.tryLock()) {
    return false;
  }
  boolean satisfied = false;
  try {
    return satisfied = guard.isSatisfied();
  } finally {
    if (!satisfied) {
      lock.unlock();
    }
  }
}
 
源代码3 项目: tddl5   文件: LockPerfMain.java
public void tReentrantLock() {
    System.currentTimeMillis();
    ReentrantLock lock = new ReentrantLock();

    long t1 = System.currentTimeMillis();
    for (int i = 0; i < 10000000; i++) {
        if (lock.tryLock()) try {
            // ...
        } finally {
            lock.unlock();
        }
    }
    long t2 = System.currentTimeMillis();

    System.out.println("take time:" + (t2 - t1) + " ms.");
}
 
源代码4 项目: Jupiter   文件: CopyOnWriteGroupList.java
public final boolean setWeightArray(JChannelGroup[] snapshot, String directory, Object weightArray) {
    if (weightArray == null || snapshot != tabAt0(array)) {
        return false;
    }
    final ReentrantLock lock = this.lock;
    boolean locked = lock.tryLock();
    if (locked) { // give up if there is competition
        try {
            if (snapshot != tabAt0(array)) {
                return false;
            }
            setWeightArray(directory, weightArray);
            return true;
        } finally {
            lock.unlock();
        }
    }
    return false;
}
 
源代码5 项目: codebuff   文件: Monitor.java
/**
 * Enters this monitor if it is possible to do so immediately and the guard is satisfied. Does not
 * block acquiring the lock and does not wait for the guard to be satisfied.
 *
 * <p><b>Note:</b> This method disregards the fairness setting of this monitor.
 *
 * @return whether the monitor was entered, which guarantees that the guard is now satisfied
 */


public boolean tryEnterIf(Guard guard) {
  if (guard.monitor != this) {
    throw new IllegalMonitorStateException();
  }
  final ReentrantLock lock = this.lock;
  if (!lock.tryLock()) {
    return false;
  }
  boolean satisfied = false;
  try {
    return satisfied = guard.isSatisfied();
  } finally {
    if (!satisfied) {
      lock.unlock();
    }
  }
}
 
源代码6 项目: openjdk-jdk9   文件: TimeoutLockLoops.java
public final void run() {
    try {
        barrier.await();
        int sum = v;
        int x = 17;
        final ReentrantLock lock = this.lock;
        while (lock.tryLock(TIMEOUT, TimeUnit.MILLISECONDS)) {
            try {
                v = x = LoopHelpers.compute1(v);
            }
            finally {
                lock.unlock();
            }
            sum += LoopHelpers.compute2(x);
        }
        barrier.await();
        result += sum;
    }
    catch (Throwable ex) {
        fail = ex;
        throw new RuntimeException(ex);
    }
}
 
源代码7 项目: Mycat2   文件: LockPerfMain.java
public void tReentrantLock() {
    System.currentTimeMillis();
    ReentrantLock lock = new ReentrantLock();

    long t1 = System.currentTimeMillis();
    for (int i = 0; i < 10000000; i++) {
        if (lock.tryLock()) {
            try {
                // ...
            } finally {
                lock.unlock();
            }
        }
    }
    long t2 = System.currentTimeMillis();

    System.out.println("take time:" + (t2 - t1) + " ms.");
}
 
源代码8 项目: codebuff   文件: Monitor.java
/**
 * Enters this monitor. Blocks at most the given time.
 *
 * @return whether the monitor was entered
 */
public boolean enter(long time, TimeUnit unit) {
  final long timeoutNanos = toSafeNanos(time, unit);
  final ReentrantLock lock = this.lock;
  if (!fair && lock.tryLock()) {
    return true;
  }
  boolean interrupted = Thread.interrupted();
  try {
    final long startTime = System.nanoTime();
    for (long remainingNanos = timeoutNanos; ; ) {
      try {
        return lock.tryLock(remainingNanos, TimeUnit.NANOSECONDS);
      } catch (InterruptedException interrupt) {
        interrupted = true;
        remainingNanos = remainingNanos(startTime, timeoutNanos);
      }
    }
  } finally {
    if (interrupted) {
      Thread.currentThread().interrupt();
    }
  }
}
 
源代码9 项目: jdk1.8-source-analysis   文件: ForkJoinTask.java
/**
 * If lock is available, poll stale refs and remove them.
 * Called from ForkJoinPool when pools become quiescent.
 */
static final void helpExpungeStaleExceptions() {
    final ReentrantLock lock = exceptionTableLock;
    if (lock.tryLock()) {
        try {
            expungeStaleExceptions();
        } finally {
            lock.unlock();
        }
    }
}
 
源代码10 项目: streamsupport   文件: ForkJoinTask.java
/**
 * If lock is available, polls stale refs and removes them.
 * Called from ForkJoinPool when pools become quiescent.
 */
static final void helpExpungeStaleExceptions() {
    final ReentrantLock lock = exceptionTableLock;
    if (lock.tryLock()) {
        try {
            expungeStaleExceptions();
        } finally {
            lock.unlock();
        }
    }
}
 
源代码11 项目: jdk8u-jdk   文件: ForkJoinTask.java
/**
 * If lock is available, poll stale refs and remove them.
 * Called from ForkJoinPool when pools become quiescent.
 */
static final void helpExpungeStaleExceptions() {
    final ReentrantLock lock = exceptionTableLock;
    if (lock.tryLock()) {
        try {
            expungeStaleExceptions();
        } finally {
            lock.unlock();
        }
    }
}
 
源代码12 项目: a-foundation   文件: ForkJoinTask.java
/**
 * If lock is available, poll stale refs and remove them.
 * Called from ForkJoinPool when pools become quiescent.
 */
static final void helpExpungeStaleExceptions() {
    final ReentrantLock lock = exceptionTableLock;
    if (lock.tryLock()) {
        try {
            expungeStaleExceptions();
        } finally {
            lock.unlock();
        }
    }
}
 
源代码13 项目: j360-dubbo-app-all   文件: ForkJoinTask.java
/**
 * If lock is available, poll stale refs and remove them.
 * Called from ForkJoinPool when pools become quiescent.
 */
static final void helpExpungeStaleExceptions() {
    final ReentrantLock lock = exceptionTableLock;
    if (lock.tryLock()) {
        try {
            expungeStaleExceptions();
        } finally {
            lock.unlock();
        }
    }
}
 
源代码14 项目: j2objc   文件: ForkJoinTask.java
/**
 * If lock is available, polls stale refs and removes them.
 * Called from ForkJoinPool when pools become quiescent.
 */
static final void helpExpungeStaleExceptions() {
    final ReentrantLock lock = exceptionTableLock;
    if (lock.tryLock()) {
        try {
            expungeStaleExceptions();
        } finally {
            lock.unlock();
        }
    }
}
 
源代码15 项目: jdk8u-dev-jdk   文件: ForkJoinTask.java
/**
 * If lock is available, poll stale refs and remove them.
 * Called from ForkJoinPool when pools become quiescent.
 */
static final void helpExpungeStaleExceptions() {
    final ReentrantLock lock = exceptionTableLock;
    if (lock.tryLock()) {
        try {
            expungeStaleExceptions();
        } finally {
            lock.unlock();
        }
    }
}
 
源代码16 项目: codebuff   文件: Monitor.java
/**
 * Enters this monitor when the guard is satisfied. Blocks at most the given time, including both
 * the time to acquire the lock and the time to wait for the guard to be satisfied, and may be
 * interrupted.
 *
 * @return whether the monitor was entered, which guarantees that the guard is now satisfied
 * @throws InterruptedException if interrupted while waiting
 */


public boolean enterWhen(Guard guard, long time, TimeUnit unit) throws InterruptedException {
  final long timeoutNanos = toSafeNanos(time, unit);
  if (guard.monitor != this) {
    throw new IllegalMonitorStateException();
  }
  final ReentrantLock lock = this.lock;
  boolean reentrant = lock.isHeldByCurrentThread();
  long startTime = 0L;
  locked:
  {
    if (!fair) {
      // Check interrupt status to get behavior consistent with fair case.
      if (Thread.interrupted()) {
        throw new InterruptedException();
      }
      if (lock.tryLock()) {
        break locked;
      }
    }
    startTime = initNanoTime(timeoutNanos);
    if (!lock.tryLock(time, unit)) {
      return false;
    }
  }
  boolean satisfied = false;
  boolean threw = true;
  try {
    satisfied =
      guard.isSatisfied()
        || awaitNanos(guard,
                      (startTime == 0L) ? timeoutNanos : remainingNanos(startTime, timeoutNanos), reentrant);
    threw = false;
    return satisfied;
  } finally {
    if (!satisfied) {
      try {
        // Don't need to signal if timed out, but do if interrupted
        if (threw && !reentrant) {
          signalNextWaiter();
        }
      } finally {
        lock.unlock();
      }
    }
  }
}
 
源代码17 项目: codebuff   文件: Monitor.java
/**
 * Enters this monitor when the guard is satisfied. Blocks at most the given time, including both
 * the time to acquire the lock and the time to wait for the guard to be satisfied, and may be
 * interrupted.
 *
 * @return whether the monitor was entered, which guarantees that the guard is now satisfied
 * @throws InterruptedException if interrupted while waiting
 */
public boolean enterWhen(Guard guard, long time, TimeUnit unit) throws InterruptedException {
  final long timeoutNanos = toSafeNanos(time, unit);
  if (guard.monitor != this) {
    throw new IllegalMonitorStateException();
  }
  final ReentrantLock lock = this.lock;
  boolean reentrant = lock.isHeldByCurrentThread();
  long startTime = 0L;

  locked:
  {
    if (!fair) {
      // Check interrupt status to get behavior consistent with fair case.
      if (Thread.interrupted()) {
        throw new InterruptedException();
      }
      if (lock.tryLock()) {
        break locked;
      }
    }
    startTime = initNanoTime(timeoutNanos);
    if (!lock.tryLock(time, unit)) {
      return false;
    }
  }

  boolean satisfied = false;
  boolean threw = true;
  try {
    satisfied =
        guard.isSatisfied()
            || awaitNanos(
                guard,
                (startTime == 0L) ? timeoutNanos : remainingNanos(startTime, timeoutNanos),
                reentrant);
    threw = false;
    return satisfied;
  } finally {
    if (!satisfied) {
      try {
        // Don't need to signal if timed out, but do if interrupted
        if (threw && !reentrant) {
          signalNextWaiter();
        }
      } finally {
        lock.unlock();
      }
    }
  }
}
 
源代码18 项目: limiter   文件: LocalLockServiceImpl.java
@Override
public boolean tryLock(String source) {
    ReentrantLock reentrantLock = initAndGetLock(source);
    return reentrantLock.tryLock();
}
 
源代码19 项目: codebuff   文件: Monitor.java
/**
 * Enters this monitor when the guard is satisfied. Blocks at most the given time, including both
 * the time to acquire the lock and the time to wait for the guard to be satisfied, and may be
 * interrupted.
 *
 * @return whether the monitor was entered, which guarantees that the guard is now satisfied
 * @throws InterruptedException if interrupted while waiting
 */


public boolean enterWhen(Guard guard, long time, TimeUnit unit) throws InterruptedException {
  final long timeoutNanos = toSafeNanos(time, unit);
  if (guard.monitor != this) {
    throw new IllegalMonitorStateException();
  }
  final ReentrantLock lock = this.lock;
  boolean reentrant = lock.isHeldByCurrentThread();
  long startTime = 0L;
  locked:
  {
    if (!fair) {
      // Check interrupt status to get behavior consistent with fair case.
      if (Thread.interrupted()) {
        throw new InterruptedException();
      }
      if (lock.tryLock()) {
        break locked;
      }
    }
    startTime = initNanoTime(timeoutNanos);
    if (!lock.tryLock(time, unit)) {
      return false;
    }
  }
  boolean satisfied = false;
  boolean threw = true;
  try {
    satisfied =
      guard.isSatisfied()
        || awaitNanos(guard,
                      (startTime == 0L) ? timeoutNanos : remainingNanos(startTime, timeoutNanos), reentrant);
    threw = false;
    return satisfied;
  } finally {
    if (!satisfied) {
      try {
        // Don't need to signal if timed out, but do if interrupted
        if (threw && !reentrant) {
          signalNextWaiter();
        }
      } finally {
        lock.unlock();
      }
    }
  }
}
 
源代码20 项目: codebuff   文件: Monitor.java
/**
 * Enters this monitor when the guard is satisfied. Blocks at most the given time, including both
 * the time to acquire the lock and the time to wait for the guard to be satisfied, and may be
 * interrupted.
 *
 * @return whether the monitor was entered, which guarantees that the guard is now satisfied
 * @throws InterruptedException if interrupted while waiting
 */


public boolean enterWhen(Guard guard, long time, TimeUnit unit) throws InterruptedException {
  final long timeoutNanos = toSafeNanos(time, unit);
  if (guard.monitor != this) {
    throw new IllegalMonitorStateException();
  }
  final ReentrantLock lock = this.lock;
  boolean reentrant = lock.isHeldByCurrentThread();
  long startTime = 0L;
  locked:
  {
    if (!fair) {
      // Check interrupt status to get behavior consistent with fair case.
      if (Thread.interrupted()) {
        throw new InterruptedException();
      }
      if (lock.tryLock()) {
        break locked;
      }
    }
    startTime = initNanoTime(timeoutNanos);
    if (!lock.tryLock(time, unit)) {
      return false;
    }
  }
  boolean satisfied = false;
  boolean threw = true;
  try {
    satisfied = guard.isSatisfied()
      || awaitNanos(guard, (startTime == 0L) ? timeoutNanos : remainingNanos(startTime, timeoutNanos), reentrant);
    threw = false;
    return satisfied;
  } finally {
    if (!satisfied) {
      try {
        // Don't need to signal if timed out, but do if interrupted
        if (threw && !reentrant) {
          signalNextWaiter();
        }
      } finally {
        lock.unlock();
      }
    }
  }
}