I found that the write lock of the ReentrantReadWriteLock provides an isHeldByCurrentThread() method to check whether the invoking thread holds that lock.

But there is no corresponding isHeldByCurrentThread() method for the read lock. Why not?

有帮助吗?

解决方案

I think the answer is in the comment of Doug Leas that he gave for this issue: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6207928.

Doug Lea writes:

The current design and behavior are intentional. Read-locks are normally not defined to have a notion of ownership, so ownership cannot be tested. … The JSR166 EG has received a few requests to optionally support per-thread read-hold tracking. Doing this would significantly increase lock overhead, so would need to be governed by an optional construction parameter. We are looking into it.

其他提示

ReentrantReadWriteLock.getReadHoldCount() seems to do the job.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top