How to detect thread being blocked by IO?

2020-05-21 08:42发布

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?

2条回答
SAY GOODBYE
2楼-- · 2020-05-21 09:07
  • 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

查看更多
狗以群分
3楼-- · 2020-05-21 09:09

You can check the statckTraces of the thread then find if the last stack is in some specific method associated with i/o blocking (eg: java.net.SocketInputStream.socketRead0)

This is not a clever way but it works.

JProfiler supports the feature you need, details show at: WHAT'S NEW IN JPROFILER 3.1

查看更多
登录 后发表回答