AlgoMaster Logo

Deadlock, Livelock, Starvation

16 min readUpdated December 16, 2025
Listen to this chapter
Unlock Audio

Deadlock, Livelock, and Starvation

Two people meet in a narrow hallway. Each steps aside to let the other pass. They step the same direction. They step again. Same direction. They're stuck in an endless dance, both being polite but neither making progress.

This is livelock. It's one of three concurrency hazards that can bring your application to a halt, along with deadlock and starvation.

These problems don't crash your program. They're worse. Your program keeps running but stops making progress. Understanding these hazards is essential for writing correct concurrent code.

In this article, we'll explore:

  • What is Deadlock and how to prevent it
  • What is Livelock and how it differs
  • What is Starvation and what causes it
  • How to detect and resolve each problem
  • Best practices for avoidance

If you're enjoying this newsletter and want to get even more value, consider becoming a [paid subscriber](https://blog.algomaster.io/subscribe).

As a paid subscriber, you'll unlock all premium articles and gain full access to all [premium courses](https://algomaster.io/newsletter/paid/resources) on [algomaster.io](https://algomaster.io).

Unlock Full Access

1. Deadlock

A deadlock occurs when two or more threads are blocked forever, each waiting for a resource held by another.

The Classic Example

Consider two threads transferring money between accounts:

Thread 1 holds Lock A and waits for Lock B. Thread 2 holds Lock B and waits for Lock A. Neither can proceed. They're deadlocked.

The Four Conditions for Deadlock

Deadlock can only occur when ALL four conditions hold simultaneously:

Scroll
Condition
Description
Example

Mutual Exclusion

Only one thread can hold the resource

Lock can only be held by one thread

Hold and Wait

Thread holds resources while waiting for others

Hold Lock A, wait for Lock B

No Preemption

Resources can't be forcibly taken

Can't force another thread to release lock

Circular Wait

Circular chain of threads waiting

T1→T2→T3→T1

Break any one condition and deadlock becomes impossible.

2. Deadlock Example

Here's a complete deadlock example:

Output:

3. Preventing Deadlock

Strategy 1: Lock Ordering

Always acquire locks in a consistent global order. This breaks the circular wait condition.

Strategy 2: Lock Timeout

Use tryLock with timeout. If you can't get all locks, release what you have and retry.

Strategy 3: Single Lock

Use a single lock for all related resources. This eliminates hold-and-wait.

Trade-off: Reduces concurrency since only one transfer can happen at a time.

Strategy 4: Avoid Nested Locks

Design your code to avoid acquiring multiple locks.

4. Detecting Deadlock

JVM Thread Dump

In Java, you can detect deadlocks using thread dumps:

Deadlock Detection Algorithm

The system can build a resource allocation graph and check for cycles:

If the graph has a cycle, deadlock exists.

5. Livelock

A livelock occurs when threads are not blocked but still can't make progress because they keep responding to each other.

Unlike deadlock where threads are frozen, in livelock threads are active but stuck in an endless loop of action and reaction.

The Hallway Problem

Livelock Example

Both diners keep passing the spoon to each other, being polite. Neither ever eats.

Livelock vs Deadlock

Scroll
Aspect
Deadlock
Livelock

Thread state

Blocked (waiting)

Active (running)

CPU usage

Low

High

Progress

None

None

Detection

Easier (blocked threads)

Harder (threads look active)

Cause

Circular wait for locks

Threads respond to each other

6. Preventing Livelock

Strategy 1: Random Backoff

Add randomness to break the symmetry:

Strategy 2: Priority/Ordering

Give one thread priority to break the tie:

Strategy 3: Retry Limits

Limit the number of retries before taking alternative action:

7. Starvation

Starvation occurs when a thread is unable to gain access to shared resources because other threads are constantly acquiring them first.

The thread isn't deadlocked. It's just perpetually waiting while others cut in line.

Causes of Starvation

  1. Thread Priorities: High-priority threads always run before low-priority ones
  2. Unfair Locks: Non-fair locks let threads skip the queue
  3. Long-Holding Threads: Some threads hold locks for extended periods
  4. Greedy Threads: Threads that repeatedly acquire resources

Starvation Example

The low-priority thread might never get the lock because high-priority threads keep acquiring it.

8. Preventing Starvation

Strategy 1: Fair Locks

Use fair locks that serve threads in FIFO order:

Strategy 2: Avoid Thread Priorities

Don't rely on thread priorities for correctness:

Strategy 3: Resource Partitioning

Give each thread its own resources when possible:

Strategy 4: Bounded Waiting

Ensure every thread eventually gets access:

9. Comparison: Deadlock vs Livelock vs Starvation

Scroll
Aspect
Deadlock
Livelock
Starvation

Thread state

Blocked

Running

Waiting/Ready

CPU usage

Low

High

Varies

Progress

None

None

Some threads progress

Affected threads

All in cycle

All involved

Only some threads

Detection

Easy

Hard

Hard

Resolution

Break cycle

Add randomness

Use fair scheduling

10. Real-World Scenarios

Deadlock: Database Transactions

Solution: Always update rows in the same order (by ID).

Livelock: Ethernet Collision

When two devices transmit simultaneously on Ethernet:

  1. Both detect collision
  2. Both back off
  3. Both retransmit at the same time
  4. Collision again

Solution: Exponential random backoff (each device waits a random time).

Starvation: Writer Starvation in Read-Write Lock

Solution: Use fair read-write lock: new ReentrantReadWriteLock(true).

11. Detection and Monitoring

Detecting Deadlock

Monitoring for Starvation

Track wait times:

12. Summary

Deadlock:

  • Threads blocked waiting for each other
  • Requires 4 conditions: mutual exclusion, hold-and-wait, no preemption, circular wait
  • Prevention: Lock ordering, timeouts, avoid nested locks

Livelock:

  • Threads active but making no progress
  • Threads keep reacting to each other
  • Prevention: Random backoff, priority ordering, retry limits

Starvation:

  • Thread perpetually denied resources
  • Caused by unfair scheduling or priorities
  • Prevention: Fair locks, avoid priorities, bounded waiting

Best Practices:

  1. Always acquire multiple locks in consistent order
  2. Use timeouts when acquiring locks
  3. Prefer fair locks when starvation is a concern
  4. Add randomness to break symmetry in retries
  5. Monitor for long wait times
  6. Avoid thread priority manipulation
  7. Keep critical sections short

Thank you for reading!

If you found it valuable, hit a like and consider subscribing for more such content every week.

If you have any questions or suggestions, leave a comment.

This post is public so feel free to share it.

Share

P.S. If you're enjoying this newsletter and want to get even more value, consider becoming a [paid subscriber](https://blog.algomaster.io/subscribe).

As a paid subscriber, you'll unlock all premium articles and gain full access to all [premium courses](https://algomaster.io/newsletter/paid/resources) on [algomaster.io](https://algomaster.io).

Unlock Full Access

There are [group discounts](https://blog.algomaster.io/subscribe?group=true), [gift options](https://blog.algomaster.io/subscribe?gift=true), and [referral bonuses](https://blog.algomaster.io/leaderboard) available.

Checkout my [Youtube channel](https://www.youtube.com/@ashishps_1/videos) for more in-depth content.

Follow me on [LinkedIn](https://www.linkedin.com/in/ashishps1/) and [X](https://twitter.com/ashishps_1) to stay updated.

Checkout my [GitHub repositories](https://github.com/ashishps1) for free interview preparation resources.

I hope you have a lovely day!

See you soon, Ashish

References

Images Needed

[IMAGE 1: Deadlock Circular Wait] <!-- Two or more threads in a cycle, each holding one resource and waiting for another -->

[IMAGE 2: Four Conditions for Deadlock] <!-- Visual showing all four conditions must be present -->

[IMAGE 3: Livelock Hallway] <!-- Two people stepping side to side, never passing -->

[IMAGE 4: Starvation Queue] <!-- High priority threads cutting in line, low priority waiting forever -->

[IMAGE 5: Comparison Table] <!-- Side-by-side visual of deadlock, livelock, starvation -->

[IMAGE 6: Prevention Strategies] <!-- Mapping problems to solutions -->