Questions tagged [reentrantreadwritelock]

ReentrantReadWriteLock is a Java class providing the lock for working with multiple threads.

ReentrantReadWriteLock is a Java class providing the lock for working with multiple threads.

This lock assumes that some resource can be unlocked (ready to lock before working with it), locked read only (multiple threads can access but none can modify) or locked read/write (only one thread can access and modify). Locks are claimed by calling methods on the lock object. If locks cannot be granted immediately, these calls suspend the current thread. Write lock can be downgraded to the read lock (but not in reverse).

To be sure the resource will not stay permanently locked because of exception, the lock in normally released inside the finally construct.

82 questions
1
vote
1 answer

How i can convert ReentrantReadWriteLock.readLock or ReentrantReadWriteLock.writeLock into my class objects

What I am trying to do is to get the number of readcounts hold by the current thread at a single time.I wrote a wrapper for that but my problem is that ReadLock() method is returning ReentrantReadWriteLock.WriteLock so I have no excess to the…
user882196
  • 1,691
  • 9
  • 24
  • 39
1
vote
1 answer

Why the write threads can't get the lock when ReentrantReadWriteLock is non-fair?

From this question How to understand the “non-fair” mode of ReentrantReadWriteLock?, I think all threads have the same opportunity to get the lock no matter which comes first. So I write this code to test it: public static void main(String[] args)…
Freewind
  • 193,756
  • 157
  • 432
  • 708
1
vote
1 answer

Striped ReadWriteLock throwing IllegalMonitorStateException - Attempt to unlock read lock, not locked by current thread

We are writing a concurrent multimap to store multiple values for the same key in a multi threaded application. We have extended Guava ForwardingMultimap to do the same. Put and remove methods acquire a write lock on a key and release it at the end.…
1
vote
1 answer

How to detect who holds the read lock of fair ReentrantReadWriteLock?

As for the fair ReentrantReadWriteLock, if the thread t1 holds the read lock and forgets to unlock, and the thread t2 try to acquire the write lock, then all the follow-up threads who try to acquire the read or write lock will block…
Zihe Liu
  • 159
  • 1
  • 12
1
vote
0 answers

How to handle reentrancy with StampLocks

I am converting a code with reentrant locks into stamp locks. The code consists of a scenario where readlock is inside the writelock as following example. private final ReadWriteLock lock = new ReentrantReadWriteLock(); A() { …
Janaka
  • 481
  • 4
  • 14
1
vote
1 answer

ConcurrentHashMap and ReentrantReadWriteLock

I have one thread that updates data in Map and several threads that read this data. Now my code looks like this: public class Updater { private ConcurrentMap valuesMap = new ConcurrentHashMap<>(); private ReadWriteLock…
Violetta
  • 509
  • 4
  • 12
1
vote
2 answers

WeakHashMap and ReentrantReadWriteLock

I implemented a cache with using WeakHashMap and ReentrantReadWriteLock, my code is like that: class Demo { private final ReentrantReadWriteLock LOCK = new ReentrantReadWriteLock(); private final Map CACHE = new…
梁雨生
  • 385
  • 3
  • 16
1
vote
1 answer

What's wrong with my usage of ReentrantReadWriteLock?

I have a trouble with ReentrantReadWriteLock. Thread hangs when I trying to wipe file. I have a scheduled read operation and eventual write operation(when user presses the button) which use one instance of ReentrantReadWriteLock. Next code looks…
1
vote
1 answer

Checking for readLock possession in `ReentrantReadWriteLock`

I have a few closely related questions that popped up while trying to use ReentrantReadWriteLock to control access to a fairly complex data structure that has a number of different read and write operations. As per examples in the documentation,…
1
vote
0 answers

Debugging ReentrantReadWriteLock Deadlock in Eclipse

I've got an Application which occasionally deadlocks (say once a month). Right now I managed for the first time to reproduce this condition while running in eclipse debugger. It seems as if all threads are waiting for the WriteLock to get…
Scheintod
  • 7,953
  • 9
  • 42
  • 61
1
vote
1 answer

Read and Write Lock threads waiting on Read lock

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…
Anuj
  • 51
  • 1
  • 2
1
vote
1 answer

Documentation for java.util.concurrent.locks.ReentrantReadWriteLock

Disclaimer: I'm not very good at Java and just comparing read/writer locks between C# and Java to understand this topic better & decisions behind both implementations. There is JavaDoc about ReentrantReadWriteLock. It states the following about…
Andrey Taptunov
  • 9,367
  • 5
  • 31
  • 44
1
vote
2 answers

ReadWriteLock decorator, is this code thread safe?

We're building cache stores in our app backed by memory, file, and remote services. Want to avoid explicit synchronization to keep the stores simple while using decorators for behavioral concerns like blocking. Here's a simple cache, this is just an…
raffian
  • 31,267
  • 26
  • 103
  • 174
1
vote
0 answers

Using a ReentrantReadWriteLock within another ReentrantReadWriteLock

I am not sure if I have implemented ReentrantReadWriteLock correctly so I would appreciate if you can let me know if I had done anything wrong. Scenario: Imagine that there are two steps required to make a shirt. The shirts will all be…
1
vote
1 answer

Does ReentrantReadWriteLock read while writeLock locked?

I research ReentrantReadWriteLock. I write simple code for test(I know that use Thread.sleep() can not guarantee predictable result but I think that I am lucky:)): public class RWLock { private static String val = "old"; private static…
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710