ReentrantLock#tryLock(long,TimeUnit) 实现在尝试获取锁时在做什么?假设线程 A 确实拥有锁myLock
,线程 B 调用myLock.tryLock(10,SECONDS)
,线程 B 是在休眠还是在等待?
换句话说,是这两种实现的区别:
1.
while (true)
try {
if (readLock.tryLock())
return;
MILLISECONDS.sleep(5);
}catch (InterruptedException e) {}
2.
while (true)
try {
if (readLock.tryLock(5,MILLISECONDS))
return;
}catch (InterruptedException e) {}