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
4
votes
5 answers

Is a read lock on a ReentrantReadWriteLock sufficient for concurrent reading of a RandomAccessFile

I'm writing something to handle concurrent read/write requests to a database file. ReentrantReadWriteLock looks like a good match. If all threads access a shared RandomAccessFile object, do I need to worry about the file pointer with concurrent…
Ed Mazur
  • 3,042
  • 3
  • 21
  • 21
3
votes
3 answers

Is there any WriteLock acquire priority over ReadLock in ReentrantReadWriteLock

I've read java doc: ReentrantReadWriteLock And I don't see that writeLock has any priority over readLock But also I've read topics like this: Are read and write locks in ReentrantReadWriteLock somehow related? and I see there in both answers…
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
3
votes
2 answers

Is it a good practice to wrap ConcurrentHashMap read and write operations with ReentrantLock?

I think in the implementation of ConcurrentHashMap, ReentrantLock has already been used. So there is no need to use ReentrantLock for the access of a ConcurrentHashMap object. And that will only add more synchronization overhead. Any comments?
3
votes
1 answer

ReentrantReadWriteLock. read and write acquire priority

I research ReentrantReadWriteLock snippet from java doc: The thread will not acquire the read lock until after the oldest currently waiting writer thread has acquired and released the write lock Thus As I understood. read duration- 1 time…
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
3
votes
4 answers

Resource manager with ReentrantLocks

I am trying to implement a resource handler class that assigns the resources (strings, stored in an array) to multiple clients that can try to acquire the lock on a set of resources and unlock them by an ID given by the lock method. I'm trying to…
3
votes
5 answers

Correct usage of ReentrantReadWriteLock while signalling from writer to reader?

The usage pattern arose from following reasons: I need read thread to wait for data if it is absent with conditions. Read lock does not support condition, so condition should be taken from write lock. Since read thread will wait for condition, it…
Dims
  • 47,675
  • 117
  • 331
  • 600
2
votes
0 answers

lockInterruptibly() advantages compared to lock() in Java

while trying to make a data structure (class) thread-safe with Java's ReentrantReadWriteLock() I stumbled across those 2 methods. lock() - Non-Interruptible Mode: we acquire lock in a non-interruptible mode which means even if the thread waiting in…
2
votes
2 answers

ReentrantReadWriteLock performing very bad compared to ReentrantLock

I have created 1000 threads to increment,1000 threads to decrement,1000 threads to read the value. Each increment thread ,increases value by 25000 times. Each decrement thread ,decreases value by 25000 times. Each read thread ,reads the value by…
2
votes
2 answers

When is it safe to use the readLock() method of the ReentrantReadWriteLock class?

It seems pretty clear that using readLock when reading from a file (for example), and using writeLock when writing to it is appropriate. However, if I have an operation where two values are being compared, such as: if (i == j) { …
Swifty McSwifterton
  • 2,637
  • 1
  • 30
  • 37
2
votes
1 answer

Do dormant Threads made so by the readLock() or writeLock() methods in ReentrantReadWriteLock class consume CPU cycles?

I am using Java 6, and reading through Java Concurrency in Practice. I am trying to figure out if when using these methods, if a dormant thread waiting for the lock uses any CPU cycles while it is dormant. Anyone know? Thanks! Matt
2
votes
1 answer

is lock downgrading necessary when using ReentrantReadWriteLock

There is a sample usage about lock downgrading in the doc of ReentrantReadWriteLock(see this). class CachedData { final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock(); Object data; volatile boolean cacheValid; void…
2
votes
1 answer

Reentrant readwrite lock synchronization on isWriteLocked

I am currently looking at implementing a session manager java class which provides functionality to read and refresh session tokens on demand. If the session token is being refreshed (i.e. being fetched from server) then session token readers should…
2
votes
1 answer

Java ReentrantReadWriteLock - Correct Usage?

I'm having some concurrency issues with my web application, where there is a write to the DB that is done, and there may also be simultaneous reads. The write first deletes all the rows and then inserts new ones, so there is the chance that the…
2
votes
1 answer

Why does a lock force you to wait until you actually have a lock?

In Java's implementation locking, there is no way to atomically upgrade a lock from a read lock to write lock. For example, the following code snippet fails ReentrantReadWriteLock lock = new ... lock.readLock().lock(); boolean mustWrite =…
corsiKa
  • 81,495
  • 25
  • 153
  • 204
1
vote
1 answer

ReentrantReadWriteLock delegation to parent thread

I want to submit tasks to a ForkJoinPool or ParallelArray from a thread holding a write lock. Access to our domain model is protected by checks that the current thread holds the relevant lock. To allow FJ workers to perform tasks on it (read only,…
sms1
  • 83
  • 7