Fundamentals 7 min read

Understanding Java Thread States and Using Arthas to Diagnose Thread Blocking

This article explains the six Java thread states defined in Thread.State, demonstrates how to use the Arthas diagnostic tool to identify blocked, waiting, or timed‑waiting threads, and provides practical strategies for diagnosing and optimizing thread‑blocking issues in Java applications.

IT Services Circle
IT Services Circle
IT Services Circle
Understanding Java Thread States and Using Arthas to Diagnose Thread Blocking

Java defines six thread states in the Thread.State enum: NEW (newly created), RUNNABLE (executing or ready for CPU), BLOCKED (waiting for a monitor lock), WAITING (waiting indefinitely via Object.wait() or LockSupport.park() ), TIMED_WAITING (waiting with a timeout via methods like Thread.sleep() or Object.wait(long) ), and TERMINATED (completed or exited with an exception).

Using Arthas to Diagnose Thread Blocking

Arthas, an open‑source Java diagnostic tool, can be used to inspect thread states and locate blocking points.

Start Arthas: java -jar arthas-boot.jar and select the target Java process.

View all thread states: thread The output lists each thread with its current state (e.g., BLOCKED, WAITING) and stack trace.

Filter blocked threads: thread --state BLOCKED # Show only threads in BLOCKED state

Detect deadlocks: thread -b # Report threads involved in deadlocks, if any

Inspect a specific thread's stack: thread <threadId> # Show detailed stack for the given thread The stack reveals the exact line of code where the thread is blocked.

Monitor method execution time: monitor -c 5 <fullClassName> <methodName> # Count method execution time trace <fullClassName> <methodName> # Trace method call chain These commands help identify costly operations or lock contention.

Common Causes of Thread Blocking

Lock contention (e.g., multiple threads competing for a synchronized lock or a mis‑managed ReentrantLock ).

Deadlocks where threads hold each other's required locks.

Blocking I/O such as database queries or network requests.

Waiting on conditions via wait() or Condition.await() without timely notification.

Resource exhaustion, e.g., a full thread‑pool queue.

Infinite loops or long‑running tasks that monopolize CPU.

Optimization Strategies for Reducing Thread Blocking

Reduce lock granularity: use finer‑grained locks, ConcurrentHashMap , or read‑write locks.

Adopt non‑blocking algorithms: atomic classes or CAS operations.

Avoid holding locks for extended periods; keep synchronized blocks short.

Leverage asynchronous programming (e.g., CompletableFuture , reactive frameworks) to prevent threads from waiting.

Configure thread pools appropriately for the workload.

Detect and resolve deadlocks, ensuring a consistent lock acquisition order.

Use timeout mechanisms when acquiring locks or waiting on conditions.

Optimize I/O with NIO, asynchronous I/O, or caching to reduce blocking time.

Additional considerations include using connection pools to lessen resource contention and employing distributed locks only when necessary.

debuggingJavaperformanceconcurrencyThreadArthas
IT Services Circle
Written by

IT Services Circle

Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.