我正在尝试使用以下代码锁定线程:
Lock lock = readLock ? getLock(key).readLock() : getLock(key).writeLock();
try {
boolean locked = lock.tryLock(DEFAULT_TRY_TIME, DEFAULT_TRY_TIME_UNIT); //line 3
// If false, lock is not acquired
if (!locked) {
throw new TryLockTimeoutException(
key + ": Failed to acquire " + lock + " within " + DEFAULT_TRY_TIME_STRING);
}
}
第3行在30分钟后返回false,因此抛出TryLockTimeoutException
,错误为:
com.concurrent.TryLockTimeoutException: keyIp : Failed to acquire java.util.concurrent.locks.ReentrantReadWriteLock$WriteLock@74be2cee[Unlocked] within 30MINUTES
at com.concurrent.NeAccessLockMap.acquireReadOrWriteLock(NeAccessLockMap.java:72)
请注意,锁定状态显示为Unlocked in error。
我无法理解为什么会发生这种情况?为什么即使锁是空闲的,线程也无法锁定。
答案 0 :(得分:3)
在您的示例中,您尝试获取写锁定,但读锁定已被锁定,这会阻止您获取写锁定。
因为您可以获取一个或多个读锁,或者获取单个写锁,所以当获取读锁时,写锁被标记为Unlocked
。
请尝试以下代码:
ReentrantReadWriteLock readWriteLock = new ReentrantReadWriteLock();
new Thread() {
@Override
public void run() {
readWriteLock.readLock().lock();
try {
// block the read lock
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
if (!readWriteLock.writeLock().tryLock(1, TimeUnit.SECONDS)) {
System.out.println(readWriteLock);
System.out.println(readWriteLock.readLock());
System.out.println(readWriteLock.writeLock());
}
会输出如下:
java.util.concurrent.locks.ReentrantReadWriteLock@31221be2[Write locks = 0, Read locks = 1]
java.util.concurrent.locks.ReentrantReadWriteLock$ReadLock@377dca04[Read locks = 1]
java.util.concurrent.locks.ReentrantReadWriteLock$WriteLock@728938a9[Unlocked]