Вопрос

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