Explicit locks
On This Lesson 4 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
know when ReentrantLock beats synchronized.
Planned topics
ReentrantLockofferstryLock(with timeout), interruptible locking, and fairness options.- Always unlock in
finally. ReadWriteLock/StampedLockhelp read-heavy data, but measure before adopting.Conditionreplaceswait/notifywith several wait sets.- Default to
synchronizedfor simplicity; use explicit locks for the extra features.
Draft outline details
Demonstration
if (lock.tryLock(100, TimeUnit.MILLISECONDS)) {
try { reserve(qty); } finally { lock.unlock(); }
}Check your understanding
Where must unlock() go?
Show answer
In a finally block.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.