12

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?

janetsmith
  • 8,562
  • 11
  • 58
  • 76
  • 2
    You mean programmatically? Or you got something stuck and you're trying to figure out what it is? – dimoniy Jan 02 '14 at 20:39
  • The Thread-class does not offer more informations, but maybe you use any kind of locks (for example ReentrantLock in j.u.concurrent-package offers special methods for querying queuing threads). – Meno Hochschild Jan 02 '14 at 20:43
  • @dimoniy I am looking at jvisualvm to find out area which is blocked by IO (to find opportunity to increase concurrency). However the area which is IO-blocked shown as "running", so I can't differentiate it. – janetsmith Jan 02 '14 at 20:55
  • I suspect that there's no "defined" way to do it programmatically. You could do the I/O in a separate thread and communicate via queue. You'd be able to tell when you were waiting on the queue. – Hot Licks Jan 02 '14 at 21:04
  • Three decades of multithread development, and I've never asked this question, of myself or others. If you don't know, why not? – Martin James Jan 03 '14 at 00:19

2 Answers2

15
  • 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

venergiac
  • 7,469
  • 2
  • 48
  • 70
  • Thanks. Is this the only way to detect IO blocked? The 2nd line (readBytes or socketRead0) may not be always there, it could be different name using different library. – janetsmith Jan 02 '14 at 22:18
  • BTW, thread dump is just a snapshot of running condition. I am looking for ways to monitor execution of thread, instead of a snapshot. – janetsmith Jan 02 '14 at 23:18
  • You can use ThreadMXBean to inpsect CPU usage of some RUNNABLE thread. If you use weblogic or other application servers some other MBeans can provide more information. The question is: why? I mean NIO is a non-blocking IO... – venergiac Jan 03 '14 at 08:22
  • Thanks. This give me some hints. I am checking existing application, so it might not use NIO. – janetsmith Jan 06 '14 at 21:02
  • So, Are you saying that, when IO bound thread completes its slice of OS resource distribution, it goes in `WAITING` state? – overexchange Oct 24 '17 at 14:25
2

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

ghostPM
  • 21
  • 1