liveBook · Manning

Starvation

As explained in Troubleshooting Java, Second Edition

A concurrency condition in which a thread is unfairly excluded from obtaining execution time or a required resource.

Starvation occurs when a thread is unfairly blocked and fails to obtain execution time or a required shared resource. In a lock-based application, one thread may be preferred while another waits repeatedly instead of executing. section 7.2

Investigate starvation by asking:

  • Do threads receive similar active CPU time and acquire a shared lock roughly equally often? section 7.2
  • Does sampling show a method waiting on itself, suggesting that its thread is locked? section 7.4

Producer and consumer sharing a list

Two threads use one list and cannot work on it simultaneously. If the producer and consumer are locked about 3,698 times each, the contention is balanced rather than starvation. If one thread is repeatedly preferred, reduce unnecessary consumer locking, let the producer work longer, add producer threads, or process values in batches. The right change depends on the application’s workload. section 7.2

How Troubleshooting Java, Second Edition covers it

How deep it goes
Defines unfair exclusion and traces it through sampling, lock counts, monitors, affected threads, and execution time.
Examples
Uses producer-consumer threads sharing an ArrayList, balanced lock counts, monitor histories, and aggregated profiler statistics.
What it assumes
Assumes threads, shared resources, and lock contention while introducing profiler-based investigation of unfair execution.
What you can do after
Diagnose starvation with sampling and lock profiling, then evaluate changes that reduce waiting or rebalance thread execution.
How it connects
Connects starvation with thread locks, monitors, synchronized blocks, shared resources, CPU time, and profiling.

Also explained in

Compare how 4 books explain starvation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Troubleshooting Java, Second Edition. The section links point to the parts of the book it draws on.