ReentrantLock and Locks — Concept Check
Questions on ReentrantLock and Locks. java.util.concurrent.locks provides explicit locking with more flexibility than synchronized — tryLock, timed locking, multiple conditions, and fairness.
Questions in this quiz
8 questions on ReentrantLock and Locks — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A developer uses ReentrantLock in a method but forgets to call unlock(). What is a likely consequence?
- AThe lock remains held, causing potential deadlocks.
- BThe program throws an exception immediately.
- CThe lock is released when the method exits.
- DThe lock automatically releases after method execution.
2.In a BoundedBuffer using ReentrantLock, what happens if a producer tries to add an item when the buffer is full?
- AThe producer waits on the 'notFull' condition.
- BThe producer waits on the 'notEmpty' condition.
- CThe producer adds the item and overwrites the oldest entry.
- DThe producer throws an exception immediately.
3.Which method allows a thread to attempt acquiring a lock without blocking?
- Alock()
- BtryLock()
- Cawait()
- Dunlock()
4.A service using ReadWriteLock experiences slow reads. What might be the issue?
- AOptimistic reads are being used.
- BThe lock is not reentrant.
- CToo many write locks are being held.
- DRead locks are blocking each other.
5.How does StampedLock's optimistic reading improve performance?
- AIt acquires a read lock immediately, avoiding validation.
- BIt uses a single lock for both reads and writes.
- CIt allows reads without locking, validating if necessary.
- DIt locks all reads and writes, ensuring consistency.
6.In Java, what happens if you unlock a ReentrantLock you do not own?
- AThe lock is released without error.
- BThe lock is silently ignored.
- CAn IllegalMonitorStateException is thrown.
- DThe program deadlocks.
7.A developer uses multiple Condition objects with a ReentrantLock. What advantage does this provide?
- AFine-grained control over thread signaling.
- BFaster lock acquisition.
- CSimplified lock management.
- DAutomatic deadlock prevention.
8.What is a key feature of ReadWriteLock?
- AAllows concurrent reads while serializing writes.
- BAutomatically upgrades read locks to write locks.
- CUses optimistic locking by default.
- DPrevents all deadlocks by design.