Chapter 9: Investigating locks in multithreaded architectures
This introduction previews techniques for investigating execution and performance in multithreaded applications and identifies prerequisite threading knowledge.
After this chapter you will be able to
- Thread lock purposes and risks. Explain why threads are locked and why lock behavior deserves investigation.
- Shared list monitor behavior. Trace how the producer and consumer use the shared list and explain how its monitor coordinates their synchronized blocks.
- Profiler thread timeline interpretation. Describe what the example’s profiler timeline reveals about the threads and why it does not imply they can never execute simultaneously.
- Sampling signs of locking. Explain how sampling reveals waiting and why a method’s self time can suggest a locked thread.
- VisualVM lock investigation. Describe how to start lock profiling in VisualVM and interpret the threads and monitor relationships in its results.
- Fairness and execution expectations. Explain how the example evaluates mutual exclusion and fairness, including the risk of starvation.
- Workload-specific contention improvements. Describe possible changes when producing values takes longer than consuming them and explain how to evaluate those changes.
- Waiting versus locked threads. Distinguish a thread locked at a synchronized block from one waiting to be notified by its monitor.
- Producer consumer wait coordination. Trace when the producer and consumer wait and how a change to the shared list allows a waiting thread to continue.
- Profiling wait notify performance. Explain how sampling and profiling showed that the wait/notify change made this application slower despite reducing locks.
What it builds on
- Sampling to observe executing code (chapter 7) · Using sampling to observe executing code
Section by section
Introduction
This introduction previews techniques for investigating execution and performance in multithreaded applications and identifies prerequisite threading knowledge.
9.1 Monitoring threads for locks
This section explains thread locks and uses a producer-consumer example and profiler timelines to show how synchronization affects execution.
- Thread lock purposes and risks: Explain why threads are locked and why lock behavior deserves investigation.
Why it matters: Understanding why threads wait helps you distinguish necessary coordination from a potential performance problem. - Shared list monitor behavior: Trace how the producer and consumer use the shared list and explain how its monitor coordinates their synchronized blocks.
Why it matters: The example shows how a shared monitor protects a resource while also creating waiting time. - Profiler thread timeline interpretation: Describe what the example’s profiler timeline reveals about the threads and why it does not imply they can never execute simultaneously.
Why it matters: A thread timeline can reveal waiting that may be worth reducing without implying that all thread execution is serialized.
Key terms: thread locks, thread synchronization, joining, blocking object, producer, consumer, monitor, synchronized block, Threads monitoring tab, thread timelines
9.2 Analyzing thread locks
This section uses sampling and lock profiling to identify thread contention and assess whether a multithreaded app’s execution suits its workload.
- Sampling signs of locking: Explain how sampling reveals waiting and why a method’s self time can suggest a locked thread.
Why it matters: Sampling helps you spot suspicious waiting before investigating its cause in detail. - VisualVM lock investigation: Describe how to start lock profiling in VisualVM and interpret the threads and monitor relationships in its results.
Why it matters: Lock profiling turns a suspicion of waiting into evidence about the threads and objects involved. - JProfiler lock perspectives: Describe the lock history and grouped statistics available in JProfiler.
Why it matters: Different views let you inspect individual lock events or see contention in aggregate. - Fairness and execution expectations: Explain how the example evaluates mutual exclusion and fairness, including the risk of starvation.
Why it matters: Lock counts are useful only when interpreted against what the application needs its threads to do. - Workload-specific contention improvements: Describe possible changes when producing values takes longer than consuming them and explain how to evaluate those changes.
Why it matters: A change that helps one workload may not help another, so the result needs measurement.
Key terms: sampling, total CPU time, self time, thread lock, Profiler tab, Locks button, monitor object, object reference, blocked thread, Monitor History
9.3 Analyzing waiting threads
This section distinguishes waiting from locked threads and uses profiling to evaluate a wait/notify change to a producer-consumer application.
- Waiting versus locked threads: Distinguish a thread locked at a synchronized block from one waiting to be notified by its monitor.
Why it matters: The distinction helps you interpret different kinds of thread delays. - Producer consumer wait coordination: Trace when the producer and consumer wait and how a change to the shared list allows a waiting thread to continue.
Why it matters: This example shows how explicit waiting coordinates work on a shared resource. - Profiling wait notify performance: Explain how sampling and profiling showed that the wait/notify change made this application slower despite reducing locks.
Why it matters: Measuring both waiting and locks prevents a seemingly promising concurrency change from being mistaken for an improvement.
Key terms: monitor, synchronized block, locked threads, waiting threads, notified, producer thread, consumer thread, wait(), notifyAll(), list
9.4 Summary
Where this is used later
- (chapter intro) (chapter 10) · Monitoring threads for locks
- Reading plain-text thread dumps (chapter 10) · Monitoring thread locks over an execution
- Getting a thread dump (chapter 10) · Thread locks in a multithreaded application
- Getting a thread dump using a profiler (chapter 10) · The distinction from analyzing thread locks during execution.
Concepts explained in this chapter
- lockingA synchronization mechanism that controls concurrent access to a protected resource or code region.
- samplingSampling is a profiling technique that observes selected parts of a program’s execution to reveal its behavior.
- CPU timeThe time a program spends executing instructions on the CPU.
- instrumentationA technique that inserts or applies probes to program execution to collect runtime information.
- lock monitoringProfiling that records lock-related activity, including blocked threads and the monitors involved.
- overall execution timeThe elapsed time for which a thread has been alive.
- thread lockA condition in which a thread cannot continue because it must wait for another thread or access to a synchronized resource.
- waiting threadA thread suspended while awaiting notification from the monitor on which it is waiting.
- waiting timeThe amount of time a thread spends waiting rather than executing.
- synchronizedA Java language construct that restricts execution of a code block so that only one thread at a time can hold its associated monitor.
- multithreaded programA multithreaded architecture is a program design in which multiple threads execute concurrently.
- starvationA condition in which a thread is repeatedly denied access to execution or resources it needs to make progress.
- performance issueA problem in an application’s use of resources or delivery of service, including but not limited to slow responses.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.
