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 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: https://bugs.java.com/bugdatabase/view_bug?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.
© 2022 - 2024 — McMap. All rights reserved.