我们在 ReentrantLock 上调用“lock()”,线程显然不应该被卡在那里。
在调用“lock()”之前使用断点进行调试时,第一个线程将在那里停止,程序指针将指向“Thread.exit()”。 锁对象的 toString() 表示“已解锁”,其“state”属性为“0”。 行为并不总是相同的。有时第一个线程会按预期越过锁。
userLock.lock(); //first thread sometimes gets stuck here (and the following ones as well)
//"userLock" has "state=0" and toString() says "UNLOCKED"
try {
Transaction tr = HibernateConfig.getSessionFactory().getCurrentSession().beginTransaction();
try {
execute();
tr.commit();
} catch (ConstraintViolationException e) {
//probably traces with repeated time
System.err.println(e.getMessage());
if (tr.isActive()) {
tr.rollback();
}
} catch (RuntimeException e) {
e.printStackTrace();
if (tr.isActive()) {
tr.rollback();
}
}
} catch (Throwable e) {
e.printStackTrace();
} finally {
userLock.unlock();
}
最佳答案
尝试在userLock.lock()之后放置断点;那么你应该获得获得锁的线程。 或者你可以使用 userLock.getOwner();就在 .lock() 后面,查看哪个线程获得了锁。
关于java - ReentrantLock 显示 "unlocked"但第一个线程停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18378834/