【发布时间】:2013-08-22 11:12:50
【问题描述】:
我们在 ReentrantLock 上调用“lock()”,而线程显然不应该被卡住。
当在调用“lock()”之前使用断点进行调试时,第一个线程将停在那里,程序指针指向“Thread.exit()”。 锁定对象的 toString() 表示“未锁定”,它的“状态”属性为“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();
}
【问题讨论】:
-
这听起来不太可能。也许有另一个线程在你打印它的状态之后正在窃取锁?也许尝试打印调试消息或在锁可能被锁定的每个位置设置断点。
-
没有其他锁。我们在它前面放了一个断点,所以它应该获取所有可能锁定它的线程。我没有打印状态,而是在我的线程处于断点时从调试 gui 中获取的。
标签: java concurrency reentrantlock