Skip to main content
PreparationZone Logo
PrepZone

Locks and Thread Pools

ReentrantLock, read-write and stamped locks, and the executor factories including shutdown.

Pass checkpoint below

Read these first

Why this matters

  • A lock you cannot time out of turns a slow dependency into a total outage, and tryLock with a timeout is the one-line defence.
  • Thread pool sizing is one of the few numbers in a backend service that has a principled formula rather than a guess.
  • Choosing the wrong executor factory method is how an application ends up with an unbounded queue that quietly becomes an OutOfMemoryError.

ReentrantLock

The same mutual exclusion as synchronized, with the lifecycle made explicit.

Java
Loading compiler…

The finally is not optional. synchronized releases its monitor automatically when the block exits; an explicit lock does not, and a missed unlock deadlocks everything that follows.

AspectsynchronizedReentrantLock
ReleaseAutomatic on block exitManual — you must call unlock in finally
TimeoutNot possibletryLock(time, unit)
Interruptible while waitingNolockInterruptibly()
FairnessNo ordering guaranteeOptional FIFO ordering
Wait conditionsOne, via wait/notifySeveral, via newCondition()
Can span methodsNo — block-scopedYes — lock here, unlock there
ReadabilityBetter; harder to misuseMore capable; easier to leak
  • Release

    synchronizedAutomatic on block exit
    ReentrantLockManual — you must call unlock in finally
  • Timeout

    synchronizedNot possible
    ReentrantLocktryLock(time, unit)
  • Interruptible while waiting

    synchronizedNo
    ReentrantLocklockInterruptibly()
  • Fairness

    synchronizedNo ordering guarantee
    ReentrantLockOptional FIFO ordering
  • Wait conditions

    synchronizedOne, via wait/notify
    ReentrantLockSeveral, via newCondition()
  • Can span methods

    synchronizedNo — block-scoped
    ReentrantLockYes — lock here, unlock there
  • Readability

    synchronizedBetter; harder to misuse
    ReentrantLockMore capable; easier to leak

Default to synchronized. Reach for ReentrantLock when you specifically need one of these features.

tryLock, the feature worth knowing

Java
Loading compiler…

This converts an indefinite hang into a fast, visible failure. In a request-serving application that is almost always the better outcome — a timeout is a problem you can see in a dashboard, whereas a thread blocked forever looks like nothing at all until the pool is exhausted.

ReadWriteLock

When reads vastly outnumber writes, allowing concurrent readers is a large win.

Java
Loading compiler…
  • Many readers, or one writer — never both.
  • A waiting writer blocks new readers, so readers cannot starve the writer indefinitely.
  • You cannot upgrade a read lock to a write lock; attempting it deadlocks. Release and reacquire instead.
  • StampedLock adds optimistic reading, which is faster still but considerably easier to misuse.

The rest of this guide is part of Pro

Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.

Still to come · 5 sections

  • StampedLock
  • Executors
  • Sizing a pool
  • Shutting down
  • Common misreadings

Test yourself

Answer these before moving on — recall is what makes it stick.