1
votes

I have a multi-threaded program where I have created a ReadWriteLock instance with fair value true. This application has stopped responding.

Below are our findings based on the thread dump. One thread acquired the read lock and it blocked at the DB operation during the execution. It did not release the lock for a long time.

There are three more threads other than the above thread. One thread is waiting to acquire write lock. Whereas, two other threads are waiting to acquire read lock.

Question is that why two threads are waiting for read lock? Is it happening because fair value is true and the thread requesting for write lock came earlier than the two threads requesting for read lock? Will system block the threads requesting for read locks if thread requesting for write came earlier?

1
Can you post the thread-dump? Threads will only block on the readLock if the writeLock is being held.John Vint

1 Answers

1
votes

When you set a Java ReentrantReadWriteLock's fairness value to true, an attempt to get a read lock will block if there is a thread currently waiting to get the write lock, as you surmise. From the Javadoc:

"A thread that tries to acquire a fair read lock (non-reentrantly) will block if either the write lock is held, or there is a waiting writer thread."

In your situation, once the current read completes, the thread waiting to write will be allowed to write, then the other threads waiting to read will be allowed to read.