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
2 answers

ReentrantReadWriteLock hang

any idea why this constructor hangs indefinitely? I'm trying to create a thread-safe singleton. private RWLockedSingleton() { lock.writeLock().lock(); System.out.println("we're done!"); isComplete = true; …
Eddy
  • 1,662
  • 2
  • 21
  • 36
0
votes
2 answers

How i can create a wrapper over ReentrantReadWriteLock ReadLock and WriteLock

I have a ReentrantReadWriteLock. The ReentrantReadWriteLock contains ReadLock and WriteLock as subclasses. I want to extend this ReadLock and WriteLock by my custom classes as DummyReadLock and DummyWriteLock. Then I must be able to do something…
Saurabh Kumar
  • 16,353
  • 49
  • 133
  • 212
0
votes
1 answer

Can i change java.util.concurrent.locks.Lock into java.util.concurrent.locks.ReentrantReadWriteLock;

I have a method which creates the lock. ReadWriteLock lock = new ReentrantReadWriteLock(); Then I pass this object into a method using Lock Interface. method(Lock lock) inside the method I just do the following. final ReentrantReadWriteLock…
user882196
  • 1,691
  • 9
  • 24
  • 39
0
votes
0 answers

java ReentrantReadWriteLock "max lock count exceeded" troubleshooting

I'm debugging an error from the application on a ReadWriteLock, as shown below in the stack traces. For the particular ReentrantReadWriteLock, you can imagine there's a global lock in an RPC server. All RPC worker threads will acquire a read lock to…
OrlandoL
  • 898
  • 2
  • 12
  • 32
0
votes
0 answers

Handle Hash Collison in Stripe Read Write lock

We are using stripe lock for one of our implementation. We take readlock on some final constant and a writelock on key. We noticed that we ran into a deadlock because hash code of two different keys turned out to be same. And hence it is like…
arun
  • 388
  • 2
  • 17
0
votes
2 answers

ReadWrite locks in Java

While a write lock is being held, Read lock can be acquired. But not vice versa. What is the rationale for this design choice. public static void main(String[] args) { System.out.println("read to write test"); ReadWriteLock…
Vineel
  • 1,630
  • 5
  • 26
  • 47
0
votes
0 answers

Should I guard Map.clear() with readLock or writeLock of j.u.c.l.ReentrantReadWriteLock

In my code I'm using ReentrantReadWriteLock to guard some paired read/write operations: public class AgreementCache { private final ConcurrentMap agreements = new ConcurrentHashMap<>(); private final ReadWriteLock…
0
votes
0 answers

Why do read locks and write locks handle expiration time differently in Redisson?

Here are the lock scripts of read lock and write lock in Redisson. lock script of read lock lock script of write lock As we can see from the script, if read lock is reentrant and lock again, the expire time of the lock will be reset to the…
chaos
  • 1,359
  • 2
  • 13
  • 25
0
votes
0 answers

Java atomicity / proper use of synchronizers & atomicity to achieve a simple use case

Working in a very simple use case challenge( just something self inflicted) Use case: A collection, that just writes data to it. The caveat is : once it reaches a certain size (eg:25 size), the data currently in it should be flushed out( say written…
0
votes
4 answers

what happens when multiple threads want to access a ReentrantReadWriteLock?

When applying a reentrantReadWriteLock, and it is locked, what happens if another thread accesses the Lock while it is already performing another block? (Thus, before it reaches the .unlock) Is the method canceled out? Or perhaps it's stalled? :O
Charlie berg
  • 171
  • 1
  • 1
  • 3
0
votes
0 answers

Is using a reentrant read write lock the fastest approach?

For a stock market algorithm, I am implementing an observable pattern for handling quotes. The "observers" are a list of triggers. The issue, is that this list can change (add a trigger, remove a trigger, etc), so I have to deal with concurrent…
0
votes
1 answer

Invoking a synchronized method of the object while aquiring ReentrantReadWriteLock.ReadLock on another thread

Thanks for considering my question, which I think is actually asking: Not quite sure how exactly below code can deadlock. The structure roughly looks like this, which has 2 classes : The main class - Worker which has synchronized method A util…
0
votes
2 answers

ReentrantReadWriteLock gets stuck on unlock

I have a class that is used to acquire and release locks for files. I use a customKey class that is just a ReentrantReadWriteLock with an id string (id being the file). For some reason, this only works in some situations, and in most it hangs on…
Luk164
  • 657
  • 8
  • 22
0
votes
1 answer

Using third-party code that contains ReentrantReadWriteLock in coroutines

I'm using a third-party library that uses ReentrantReadWriteLock internally for synchronization. Of course, the library has no suspending function. For my project I'm using coroutines. Is it safe to use the library with coroutines suspending…
0
votes
1 answer

Java Timer scheduleAtFixedRate. How to make it so that calling .cancel will stop the timer only after the current iteration is done

I have a ReentrantReadWriteLock in my application. In the run method of the timertask, I do write lock and then call a function. After that I unlock: timer.scheduleAtFixedRate(new TimerTask() { public void run() { …
LTM
  • 527
  • 1
  • 9
  • 18