gpt4 book ai didi

java - 不稳定的 StampedLock.unlock(long) 行为?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:09:04 27 4
gpt4 key购买 nike

我正面临关于 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,读取器计数of 2. 请注意,邮票的状态编号是相同的,因为 StampedLock 的状态在邮票的获取之间没有改变。第一个读取标记用于解锁。第一个图章 (4) 的州编号与 StampedLock (4) 的州编号相匹配,所以没问题。第一个标记 (1) 的读取器计数与 StampedLock (2) 的读取器计数不匹配,但这无关紧要,因为读锁应该能够以任何顺序解锁。至此解锁成功。

请注意 StampedLocks were designed to be high-performing read/write locks对于内部实用程序,无法承受恶意编码,因此它在其预期范围内运行。我确实认为 unlock() 的 Javadoc虽然具有误导性。

关于java - 不稳定的 StampedLock.unlock(long) 行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30647525/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com