liveBook · Manning

Locked thread

As explained in Troubleshooting Java, Second Edition

A locked thread is a thread prevented from entering or executing a synchronized region because another thread holds the relevant monitor.

A locked thread is blocked at the entrance of a synchronized block because another thread currently holds the relevant monitor. The monitor permits only one thread to execute inside that restricted region at a time. A locked thread differs from a waiting thread: waiting begins after the monitor explicitly suspends a thread already inside the synchronized region. section 7.3

Figure 7.14 Locked versus waiting threads. A locked thread is blocked at the entrance of a synchronized block. The monitor won’t allow a thread to enter a synchronized block while another thread actively runs inside the block. A waiting thread is a thread that the monitor has explicitly set to the blocked state. The monitor can make any thread inside the synchronized block it manages wait. The waiting thread can continue its execution only after the monitor explicitly tells it that it can proceed.

Use this comparison to distinguish contention at a synchronized block’s entrance from explicit waiting inside the block. section 7.3

In a producer–consumer application:

  • A consumer can wait when the list is empty, allowing a producer to acquire the monitor and add a value. section 7.3
  • A producer can wait when the list reaches its capacity, allowing a consumer to remove values. section 7.3
  • Profiling may show fewer lock events but longer blocking periods, so frequency and duration both matter. section 7.3

How Troubleshooting Java, Second Edition covers it

How deep it goes
Defines monitor entry blocking, contrasts waiting, and interprets sampled lock frequency and blocking duration.
Examples
Uses producer and consumer threads sharing a list, with empty-list and full-list waiting scenarios.
What it assumes
Assumes synchronized blocks and monitors, while defining the difference between locked and waiting states.
What you can do after
Diagnose contention by distinguishing locked from waiting threads and comparing lock frequency with blocking duration.
How it connects
Connects synchronized blocks, monitors, waiting threads, notification, and producer-consumer coordination.

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.