Skip to main content
1:1 mock interviews with Sumit, scored on a real hiring rubric.Book a slot →
TOPIC 5 OF 5 · 2 MIN READ

Locks, Atomic Classes and Deadlocks

Beyond synchronized, java.util.concurrent offers ReentrantLock, read-write locks, atomic classes built on compare-and-swap, and synchronizers like CountDownLatch and Semaphore. Deadlock causes and prevention are classic interview questions.

What you will learn

  • Compare ReentrantLock and synchronized
  • Use AtomicInteger, LongAdder and CAS
  • Know CountDownLatch, CyclicBarrier and Semaphore
  • State the four deadlock conditions and how to prevent them

Key concepts

ReentrantLock

An explicit lock with tryLock (with timeout), interruptible locking, optional fairness and multiple Conditions. Always unlock in finally.

CAS

Compare-and-swap: update a value only if it still equals what you read, retrying otherwise. AtomicInteger.incrementAndGet uses it without locks. LongAdder spreads contention across cells for hot counters.

Synchronizers

CountDownLatch waits for N events once. CyclicBarrier lets N threads wait for each other repeatedly. Semaphore limits how many threads use a resource at once.

Deadlock

Needs all four: mutual exclusion, hold and wait, no preemption, circular wait. Break any one, usually circular wait by always taking locks in the same global order, or use tryLock with timeouts.

Step by step

A deadlock and its fix

// Thread 1: transfer(a, b)   Thread 2: transfer(b, a)
void transfer(Account from, Account to, BigDecimal amt) {
    synchronized (from) { synchronized (to) { move(from, to, amt); } }   // can deadlock
}

// Fix: lock in a consistent order
void transferSafe(Account from, Account to, BigDecimal amt) {
    Account first = from.id() < to.id() ? from : to;
    Account second = first == from ? to : from;
    synchronized (first) { synchronized (second) { move(from, to, amt); } }
}

Examples

ReentrantLock with tryLock

private final ReentrantLock lock = new ReentrantLock();

boolean update() throws InterruptedException {
    if (!lock.tryLock(200, TimeUnit.MILLISECONDS)) return false;   // give up instead of waiting forever
    try {
        // critical section
        return true;
    } finally {
        lock.unlock();
    }
}

Practise it

Try these without looking back. If you can explain each one out loud, you are ready for this question.

  • Use a CountDownLatch to start 10 worker threads at exactly the same moment.
  • Find a deadlock in a thread dump (jstack) and explain it.

Interview questions

What interviewers ask next on this topic. Answer out loud first, then open the model answer.

Deadlock vs livelock vs starvation?

Deadlock: threads wait for each other forever and nothing moves. Livelock: threads keep reacting to each other and stay busy but make no progress. Starvation: a thread never gets the resource because others always win, such as with an unfair lock.

What is the ABA problem?

With CAS, a value changes from A to B and back to A, so a CAS that expects A succeeds even though the state changed in between. AtomicStampedReference adds a version stamp to detect it.

Summary

Use ReentrantLock when you need tryLock or conditions, atomics and LongAdder for counters, synchronizers for coordination, and prevent deadlocks with consistent lock ordering and timeouts.

Stuck on this one? Talk to Sumit.

Keys: F focus mode · ← → previous / next topic · Esc leave focus