Please enable JavaScript.
Coggle requires JavaScript to display documents.
11.3 Costs Introduced by Threads - Coggle Diagram
11.3 Costs Introduced by Threads
Context Switching
Definition: Happens when more threads than CPUs are runnable; OS preempts threads.
Cause:
Thread blocking (I/O, contended locks, condition waits)
Cost:
~5,000–10,000 clock cycles (several microseconds)
Increases scheduling overhead and reduces throughput
Indicators:
High kernel time usage (over 10%)
Tools:
vmstat (Unix)
perfmon (Windows)
Optimization:
Use nonblocking algorithms to reduce context switches
Memory Synchronization
Sources of Cost:
Memory barriers:
Flush/invalidate caches
Stall pipelines
Inhibit compiler optimizations
Uncontended vs. Contended Synchronization:
Uncontended:
Fast (20–250 clock cycles)
JVM optimized
Contended:
Costlier, may involve OS
JVM Optimizations:
Escape Analysis: Identifies thread-local objects
Lock Elision: Removes locks only accessed by one thread
Lock Coarsening: Merges adjacent synchronized blocks
Shared Memory Bus Traffic:
Synchronization creates bus contention
Heavy contention → degraded performance for all threads
Nonblocking algorithms can worsen this without backoff mechanisms
Blocking
Occurs When:
Lock is contended
Thread performs blocking I/O or condition waits
Blocking Strategies:
Spin-waiting:
Repeated lock attempts
Best for short waits
Suspension:
OS suspends thread
Best for long waits
JVM Strategies:
Some use adaptive strategies based on profiling
Most suspend threads by default
Cost of Blocking:
Two context switches:
Thread switched out early
Switched back in later
Cache and OS activity
Lock holder must notify OS to resume blocked thread
Key Insight
Don't over-optimize uncontended synchronization
It is already efficient
Focus on areas with lock contention