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
75
votes
12 answers

Java ReentrantReadWriteLocks - how to safely acquire write lock when in a read lock?

I am using in my code at the moment a ReentrantReadWriteLock to synchronize access over a tree-like structure. This structure is large, and read by many threads at once with occasional modifications to small parts of it - so it seems to fit the…
Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
58
votes
5 answers

ReentrantReadWriteLock: what's the difference between ReadLock and WriteLock?

What I know is: ReadLock and WriteLock affect each other somehow WriteLock is just like synchronized ReadLock seems cannot work alone
DunkOnly
  • 1,682
  • 4
  • 17
  • 39
12
votes
3 answers

ReentrantReadWriteLock - many readers at a time, one writer at a time?

I'm somewhat new to multithreaded environments and I'm trying to come up with the best solution for the following situation: I read data from a database once daily in the morning, and stores the data in a HashMap in a Singleton object. I have a…
Sarah
  • 245
  • 2
  • 6
  • 12
10
votes
1 answer

Why isn't ReadWriteLock upgrade allowed?

ReadWriteLock downgrade is allowed by ReentrantReadWriteLock implementation (tryLock() from the example below always returns true): void downgrade(final ReadWriteLock readWriteLock) { boolean downgraded = false; …
Bass
  • 4,977
  • 2
  • 36
  • 82
10
votes
3 answers

ConcurrentHashMap vs ReentrantReadWriteLock based Custom Map for Reloading

Java Gurus, Currently we have a HashMap which is being read frequently and modified occasionally and we are having issues that during the modification/reloading, Read operation returns null which is not acceptable. To…
Bharat Sinha
  • 13,973
  • 6
  • 39
  • 63
7
votes
1 answer

Will Readlock and Writelock cause starvation for writer?

In solving reader write problem, I try to use ReentrantReadWriteLock. I know that all readers can acquire the read lock at the same time, however, write lock has to be wait for all the read locks to be released. Will this cause the writer to be in…
Adam Lee
  • 24,710
  • 51
  • 156
  • 236
7
votes
3 answers

Is it safe to lock multiple ReentrantReadWriteLocks in the same try block?

Let's say I have two critial resources, foo and bar. I protect them with some ReentrantReadWriteLocks ReentrantReadWriteLock foo = new RRWL() ... ReentrantReadWriteLock bar = new RRWL() ... Most operations only use foo OR bar, but some of them…
corsiKa
  • 81,495
  • 25
  • 153
  • 204
7
votes
1 answer

scala collections circular buffer

Just messing about here, with circular buffers. Is this a sensible implementation or is there a faster/more reliable way to skin this cat? class CircularBuffer[T](size: Int)(implicit mf: Manifest[T]) { private val arr = new…
irishjava
  • 503
  • 4
  • 11
7
votes
2 answers

Java : ReentrantReadWriteLock with priority

The following is the typical reader and writer pattern (a lot of reads and few writes) private ReadWriteLock lock = new ReentrantReadWriteLock(); private int value; public void writeValue(int newValue){ lock.writeLock().lock(); …
6
votes
2 answers

Are read and write locks in ReentrantReadWriteLock somehow related?

Please explain me more the contract. I can't figure out if two locks contained in ReentrantReadWriteLock somehow related? Or these are just a bundle of two normal locks?
Dims
  • 47,675
  • 117
  • 331
  • 600
6
votes
4 answers

How to wait for data with ReentrantReadWriteLock?

It is said, that ReentrantReadWriteLock is intended for one writer and multiple readers. Nevertheless, readers should wait until some data is present in the buffer. So, what to lock? I created concurrency objects like follows: private final…
Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385
5
votes
0 answers

My thread acquires the read lock but when trying to release it IllegalMonitorStateException is thrown

my application is deployed on Weblogic 10.3.5 with java 6 update 30. I encountered with the following error while executed this code lines: lock.readLock().lock(); try { holder = cache.get(configName); // If it exists in the…
Maxim Kirilov
  • 2,639
  • 24
  • 49
4
votes
1 answer

Reentrant locks within monads in Scala

A colleague of mine stated the following, about using a Java ReentrantReadWriteLock in some Scala code: Acquiring the lock here is risky. It's "reentrant", but that internally depends on the thread context. F may run different stages of the same…
4
votes
2 answers

ReentrantReadWriteLock - why can't reader acquire writer's lock?

In ReentrantReadWriteLock documentation it is said: writer can acquire the read lock, but not vice-versa If I understand correctly it means that from the same thread you can…
awfun
  • 2,316
  • 4
  • 31
  • 52
4
votes
1 answer

Java: Difference between ReadWriteLock and ReentrantReadWriteLock

I have some data structure, in which I want to exclusively lock the access for writing, but to enable parallel access for reading. I made some searches and found out the classes ReadWriteLock and ReentrantReadWriteLock - which both supply a…
SomethingSomething
  • 11,491
  • 17
  • 68
  • 126
1
2 3 4 5 6