How to detect thread being blocked by IO?

后端 未结 2 587
隐瞒了意图╮
隐瞒了意图╮ 2021-02-03 12:13

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 \"RU

相关标签:
2条回答
  • 2021-02-03 12:38

    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

    0 讨论(0)
  • 2021-02-03 12:44
    • 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

    0 讨论(0)
提交回复
热议问题