Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect thread being blocked by IO?

In Java, thread can have different state:

NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED

However, when the thread is blocked by IO, its state is "RUNNABLE". How can I tell if it is blocked by IO?

like image 631
janetsmith Avatar asked Jan 02 '14 20:01

janetsmith


People also ask

How do I see blocked threads?

You can identify blocked threads on the Thread Contention (THRDCON) view. On THRDCON you can identify threads that are in blocked state. A blocked state exists when one method is waiting for a Java resource held by another thread in the same JVM.

Why do threads block on IO?

Java IO's various streams are blocking. It means when the thread invoke a write() or read(), then the thread is blocked until there is some data available for read, or the data is fully written.

Does blocking IO use CPU?

Some I/O requests are blocked, which means control does not return to the application until the I/O task is over. In such cases, the CPU has to run multiple threads/processes to complete other tasks.

Can a thread be blocked?

The running thread will block when it must wait for some event to occur (response to an IPC request, wait on a mutex, etc.). The blocked thread is removed from the running array, and the highest-priority ready thread that's at the head of its priority's queue is then allowed to run.


1 Answers

  • NEW: The thread is created but has not been processed yet.
  • RUNNABLE: The thread is occupying the CPU and processing a task. (It may be in WAITING status due to the OS's resource distribution.)
  • BLOCKED: The thread is waiting for a different thread to release its lock in order to get the monitor lock. JVISULVM shows thta as Monitoring
  • WAITING: The thread is waiting by using a wait, join or park method.
  • TIMED_WAITING: The thread is waiting by using a sleep, wait, join or park method. (The difference from WAITING is that the maximum waiting time is specified by the method parameter, and WAITING can be relieved by time as well as external changes.)
  • TERMINATED: A thread that has exited is in this state.

see also http://architects.dzone.com/articles/how-analyze-java-thread-dumps

Thread Dump

Dumping java thread stack you can find something like that

   java.lang.Thread.State: RUNNABLE
           at java.io.FileInputStream.readBytes(Native Method)

or

  java.lang.Thread.State: RUNNABLE
          at java.net.SocketInputStream.socketRead0(Native Method)

and you can understand that java is waiting response.

I suggest this tool Java Thread Dump Analyser or this plug-in TDA

ThreadMXBean

Yiu can obtain more information using the ThreadMXBean

http://docs.oracle.com/javase/7/docs/api/java/lang/management/ThreadMXBean.html

like image 118
venergiac Avatar answered Oct 05 '22 22:10

venergiac