Skip to content
Ameen Abozaid
Planned lessonMID-004-L04

Explicit locks

  • Lesson 4 of 9
  • Mid-level
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

  • ReentrantLock offers tryLock (with timeout), interruptible locking, and fairness options.
  • Always unlock in finally.
  • ReadWriteLock/StampedLock help read-heavy data, but measure before adopting.
  • Condition replaces wait/notify with several wait sets.
  • Default to synchronized for simplicity; use explicit locks for the extra features.
Draft outline details

Demonstration

java
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.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.