Reputation: 181
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
Upvotes: 0
Views: 1197
Reputation: 7896
Since you said ReentrantReadWriteLock, the behavior depends on whether you're talking about taking the read lock or the write lock associated with the ReadWriteLock.
The read lock can be held concurrently by multiple threads as long as there is no writer.
Upvotes: 2
Reputation: 533880
If you don't want to block you can use Lock.tryLock()
(which tries without waiting) or tryLock(long time, TimeUnit unit)
which will wait only as long as you specify.
Upvotes: 0
Reputation: 341003
The thread will block. If more than one thread tries to acquire this lock, all of them will be blocked. When the lock is released, exactly one thread from the waiting pool will acquire the lock and the rest will still wait. See the difference between fair and unfair locks.
Upvotes: 1
Reputation: 40871
The thread will block until the lock is available. (docs)
If you only want to acquire the lock if its available, you can use tryLock()
Upvotes: 1