下面列出了java.util.concurrent.locks.ReentrantLock#tryLock() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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();
}
}
}
/**
* 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();
}
}
}
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.");
}
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;
}
/**
* 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();
}
}
}
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);
}
}
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.");
}
/**
* 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();
}
}
}
/**
* 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();
}
}
}
/**
* 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();
}
}
}
/**
* 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();
}
}
}
/**
* 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();
}
}
}
/**
* 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();
}
}
}
/**
* 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();
}
}
}
/**
* 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();
}
}
}
/**
* 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();
}
}
}
}
/**
* 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();
}
}
}
}
@Override
public boolean tryLock(String source) {
ReentrantLock reentrantLock = initAndGetLock(source);
return reentrantLock.tryLock();
}
/**
* 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();
}
}
}
}
/**
* 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();
}
}
}
}