锁-StampedLock知识补充 - #2892
Open
sharpener-YuFeng wants to merge 1 commit into
Open
Conversation
原句比较笼统
Snailclimb
requested changes
Aug 5, 2026
Snailclimb
left a comment
Owner
There was a problem hiding this comment.
感谢补充,原文把“当前线程持锁后再次获取一定会返回新 stamp”一概而论,确实不准确。不过当前改法还需要调整后再合入:
tryWriteLock()这一点需要明确前提是“当前线程已经持有该写锁时再次调用”,否则现在的描述只是该 API 的一般返回语义。- 同一线程连续调用
readLock()可能成功,是因为读模式本身是共享的,而且StampedLock不记录线程所有权;这不能视为读锁具备可重入语义。每次成功获取仍要保存相应的 stamp 并分别释放。 - 建议删除“判断是否可重入,重点看独占写锁”。Oracle 文档直接说明
StampedLock不可重入且没有所有权概念;可重入性不宜仅靠写锁这一点来定义。 - 请补齐中英文空格和标点,例如“还是
try方法:”“使用tryWriteLock()时”“非 0 的 stamp”。
建议将这一段精简为:
StampedLock成功获取读锁或写锁后会返回一个 long 型 stamp,释放锁或转换锁模式时需要传入对应的 stamp;try系列方法获取失败时返回 0。它是不可重入锁,并且不记录锁的线程所有权。线程持有写锁时再次调用writeLock(),锁无法识别调用者就是当前持有者,因此会继续等待;调用tryWriteLock()则会立即返回 0。同一线程连续调用readLock()可能获得多个读 stamp,是因为读模式允许共享,而不是因为锁识别了线程并支持重入;每次成功获取都需要使用对应的 stamp 释放。
依据可参考 Oracle StampedLock API 文档。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
原句比较笼统