Skip to the page

Java in production · senior

Diagnosing CPU, blocked threads and thread leaks

Take two or three thread dumps a few seconds apart, then read them. A thread in the same place every time is stuck. Match hot OS threads to Java threads, and group threads by name.

The notes

What you must know

  • Evidence first: top -H -p <pid>, two or three thread dumps a few seconds apart, then restart. Rule out GC first: if the GC threads are hot, it is a memory problem.
  • Map a hot OS thread to Java: convert the thread id to hex (2381 is 0x94d) and find nid=0x94d in the dump. The same stack in every dump means a loop or a stuck call.
  • Catastrophic backtracking: a regex with a nested quantifier such as (...+\s?)+ on a long failing input takes exponential time. Rewrite without nested quantifiers, check length first, and cap request size.
  • Many request threads BLOCKED waiting to lock the same address: lock contention. Search the dump for "locked" with that address to find the owner.
  • Typical cause: a synchronized method that does a network call. Every request waits behind one slow call. Never hold a lock across network I/O; cache a token and refresh it in the background; add timeouts and a circuit breaker.
  • Thread count climbing into thousands: group by name. pool-N-thread-M with N climbing means a new executor is created for each call and never shut down. Use one shared executor created once and closed at shutdown.
  • RejectedExecutionException when the queue is full: catch it, keep the order saved, and make sending durable with an outbox row or an event. CallerRunsPolicy can make request threads do slow work and slow the whole API.
  • A pooled thread that keeps an InheritableThreadLocal shows one customer's data to another. Pass the customer id explicitly; copy context at submit time; ScopedValue cannot leak this way. Alerts: one pod's CPU far above the others, request-thread pool usage, executor queue size, live thread count.

The red pen

Common traps: mistake → fix

  • Mistake: High CPU always means a busy loop in my code

    Fix: check GC threads first, then the hot thread's stack.

  • Mistake: More pods fix blocked threads

    Fix: each pod blocks on the same slow dependency.

  • Mistake: A leaked InheritableThreadLocal is only a memory issue

    Fix: it can leak private data between users.

Easy to hard

Questions interviewers ask, easy to hard

  1. One of six pods is at 100 percent CPU. What do you do?
  2. P99 went from 200 ms to 12 seconds with low CPU. What do you look for in a thread dump?
  3. The live thread count grows until threads cannot be created. Why?
  4. During a campaign the API returns RejectedExecutionException. What happened and what is the proper design?
  5. A customer saw another customer's name on an invoice. How do you handle it?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview