我面临有关StampedLock的奇怪行为。以下是主要的有问题的代码行:
StampedLock lock = new StampedLock();
long stamp1 = lock.readLock();
System.out.printf("Read lock count: %d%n", lock.getReadLockCount());
lock.unlock(stamp1 + 2);
System.out.printf("Read lock count: %d%n", lock.getReadLockCount());
奇怪的行为是关于如何解锁“容忍”错误的读取标记。您觉得对吗?
供参考,下面是完整的代码:
public class StampedLockExample {
static StampedLock lock = new StampedLock();
static void println(String message, Object... args) {
System.out.printf(message, args);
System.out.println();
}
static void printReadLockCount() {
println("Lock count=%d", lock.getReadLockCount());
}
static long tryReadLock() {
long stamp = lock.tryReadLock();
println("Gets read lock (%d)", stamp);
printReadLockCount();
return stamp;
}
static long tryWriteLock() {
long stamp = lock.tryWriteLock();
println("Gets write lock (%d)", stamp);
return stamp;
}
static long tryConvertToReadLock(long stamp) {
long newOne = lock.tryConvertToReadLock(stamp);
println("Gets read lock (%d -> %d)", stamp, newOne);
printReadLockCount();
return newOne;
}
static void tryUnlock(long stamp) {
try {
lock.unlock(stamp);
println("Unlock (%d) successfully", stamp);
} catch (IllegalMonitorStateException e) {
println("Unlock (%d) failed", stamp);
}
printReadLockCount();
}
public static void main(String[] args) {
println("%n--- Gets two read locks ---");
long stamp1 = tryReadLock();
long stamp2 = tryReadLock();
long min = Math.min(stamp1, stamp2);
long max = Math.max(stamp1, stamp2);
println("%n--- Tries unlock (-1 / +2 / +4) ---");
tryUnlock(min - 1);
tryUnlock(max + 2);
tryUnlock(max + 4);
println("%n--- Gets write lock ---");
long stamp3 = tryWriteLock();
println("%n--- Tries unlock (-1 / +1) ---");
tryUnlock(stamp3 - 1);
tryUnlock(stamp3 + 1);
println("%n--- Tries write > read conversion ---");
long stamp4 = tryConvertToReadLock(stamp3);
println("%n--- Tries unlock last write stamp (-1 / 0 / +1) ---");
tryUnlock(stamp3 - 1);
tryUnlock(stamp3);
tryUnlock(stamp3 + 1);
println("%n--- Tries unlock (-1 / +1) ---");
tryUnlock(stamp4 - 1);
tryUnlock(stamp4 + 1);
}
}
输出:
--- Gets two read locks ---
Gets read lock (257)
Lock count=1
Gets read lock (258)
Lock count=2
--- Tries unlock (-1 / +2 / +4) ---
Unlock (256) failed
Lock count=2
Unlock (260) successfully
Lock count=1
Unlock (262) successfully
Lock count=0
--- Gets write lock ---
Gets write lock (384)
--- Tries unlock (-1 / +1) ---
Unlock (383) failed
Lock count=0
Unlock (385) failed
Lock count=0
--- Tries write > read conversion ---
Gets read lock (384 -> 513)
Lock count=1
--- Tries unlock last write stamp (-1 / 0 / +1) ---
Unlock (383) failed
Lock count=1
Unlock (384) failed
Lock count=1
Unlock (385) failed
Lock count=1
--- Tries unlock (-1 / +1) ---
Unlock (512) failed
Lock count=1
Unlock (514) successfully
Lock count=0
最佳答案
简短答案:
在戳记上加上两个即表示修改了它的一部分,不需要在读模式锁中进行验证。
长答案:
该图章包含两个信息:状态序列号和有多少个读者。状态编号存储在标记的前57位中,而读取器计数存储在后7位中。因此,当您在图章上添加2时,您会将阅读器的数量从1更改为3,并保持状态编号不变。由于StampedLock仅在读取模式下获取,因此仅验证状态号,而忽略读取器计数。这是有道理的,因为读取锁应该能够以任何顺序解锁。
例如:从现有的StampedLock获取读取标记,其状态编号为4,读取器计数为1。从同一StampedLock获取第二读取标记,其状态编号为4,读取器计数为2。请注意,图章的状态编号是相同的,因为在获取图章之间,StampedLock的状态没有改变。第一个读取的标记用于解锁。第一个图章(4)的状态号与StampedLock(4)的状态号相匹配,这样就可以了。第一个图章(1)的读取器计数与StampedLock(2)的读取器计数不匹配,但是没关系,因为读取锁应该能够以任何顺序解锁。因此解锁成功。
请注意,StampedLocks were designed to be high-performing read/write locks用于内部实用程序,不能承受恶意代码,因此它在预期的范围内运行。我确实认为unlock()的Javadoc具有误导性。