reentrantreadwritelock

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

岁酱吖の 提交于 2019-12-07 02:44:44
问题 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 happen to use both. Now when using a single lock, you can't just do this: void foo() { foo.writeLock().lock(); privateWorkOnFoo(); foo.writeLock().unlock(); } If an exception is thrown, your foo will become forever locked. Instead you wrap it, like void

Are read and write locks in ReentrantReadWriteLock somehow related?

天涯浪子 提交于 2019-12-05 10:27:11
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? If threads are waiting Read Lock it is shared but when thread wants to acquire write lock only that thread is allowed the access same as mutual exclusion. So either one of operation is allowed . if lock is held by readers and thread request write lock no more readers are allowed to acquire read lock until thread which has acquired write lock release it . It allows multiple threads to read a resource concurrently, but requires

scala collections circular buffer

徘徊边缘 提交于 2019-12-03 14:33:37
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 scala.collection.mutable.ArrayBuffer[T]() private var cursor = 0 val monitor = new ReentrantReadWriteLock() def push(value: T) { monitor.writeLock().lock() try { arr(cursor) = value cursor += 1 cursor %= size } finally { monitor.writeLock().unlock() } } def getAll: Array[T] = { monitor.readLock().lock() try { val copy = new Array[T](size) arr.copyToArray(copy) copy }

How to wait for data with ReentrantReadWriteLock?

一曲冷凌霜 提交于 2019-12-01 18:18:50
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 ReentrantReadWriteLock rwl = new ReentrantReadWriteLock(); protected final Lock readLock = rwl.readLock(); protected final Lock writeLock = rwl.writeLock(); protected final Condition hasData = writeLock.newCondition(); now in write method I do: writeLock.lock(); // writing first portion and updating variables hasData.signalAll(); // if required then writing

How to wait for data with ReentrantReadWriteLock?

♀尐吖头ヾ 提交于 2019-12-01 18:14:37
问题 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 ReentrantReadWriteLock rwl = new ReentrantReadWriteLock(); protected final Lock readLock = rwl.readLock(); protected final Lock writeLock = rwl.writeLock(); protected final Condition hasData = writeLock.newCondition(); now in write method I do: writeLock

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

自古美人都是妖i 提交于 2019-11-27 06:48:50
What I know is: ReadLock and WriteLock affect each other somehow WriteLock is just like synchronized ReadLock seems cannot work alone OldCurmudgeon readLock.lock(); This means that if any other thread is writing (i.e. holds a write lock) then stop here until no other thread is writing. Once the lock is granted no other thread will be allowed to write (i.e. take a write lock) until the lock is released. writeLock.lock(); This means that if any other thread is reading or writing , stop here and wait until no other thread is reading or writing. Once the lock is granted, no other thread will be

Java ReentrantReadWriteLocks - how to safely acquire write lock?

孤人 提交于 2019-11-26 23:31:36
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 read-write idiom well. I understand that with this particular class, one cannot elevate a read lock to a write lock, so as per the Javadocs one must release the read lock before obtaining the write lock. I've used this pattern successfully in non-reentrant contexts before. What I'm finding however is that I cannot reliably acquire the write lock without