Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why there is no way to check if current thread holds the read lock of ReentrantReadWriteLock?

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?

like image 226
rollaeriu360 Avatar asked Dec 18 '12 17:12

rollaeriu360


People also ask

What is the maximum number of threads that can possess the Writelock of a Reentrantreadwritelock at the same time?

This lock supports a maximum of 65535 recursive write locks and 65535 read locks.

What is the purpose of read lock?

A read lock allows multiple concurrent readers of some data, but it prevents readers from accessing the data while a writer is in the middle of changing it. That ensures that a reader will never see a partial update (a state where the writer has updated some parts of the data but not all of them.

What is read/write lock in Java?

A ReadWriteLock maintains a pair of associated locks , one for read-only operations and one for writing. The read lock may be held simultaneously by multiple reader threads, so long as there are no writers. The write lock is exclusive.


2 Answers

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.

like image 176
Miguel Gamboa Avatar answered Oct 24 '22 16:10

Miguel Gamboa


ReentrantReadWriteLock.getReadHoldCount() seems to do the job.

like image 34
ikaushan Avatar answered Oct 24 '22 14:10

ikaushan