Question

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?

Était-ce utile?

La solution

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.

Autres conseils

ReentrantReadWriteLock.getReadHoldCount() seems to do the job.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top