Search code examples
javamultithreadingconcurrencyjava-threadsjvisualvm

How to match theoretical thread states and states showed by jvisualvm


If we will google something like 'java thread state' we will see approximately this diagram:

enter image description here

But if we will open jVisualVm we will see following:

enter image description here

Can you help to meatch these diagrams?

Sleeping state is just Thread.sleep()? Special case of the Running?

What the Park state?(I tried to google but I confused because I knew before only first diagram)


Solution

  • The diagram represents java.lang.Thread.State enum. The Javadoc is quite helpful to get an understanding of the mapping you seek.

    The JVisualVM state represent the extra state description you would see in a thread dump, e.g.:

     "Finalizer" daemon prio=8 tid=0x022f4000 nid=0xd14 in Object.wait() [0x044cf000]
        java.lang.Thread.State: WAITING (on object monitor)
    

    So you could decipher the state on your own, if you get a thread dump and compare the state from JVisualVM and the thread dump by a thread name.

    Here is the mapping you want:

    • Running -> java.lang.Thread.State: RUNNABLE
    • Sleeping -> java.lang.Thread.State: TIMED_WAITING (sleeping)
    • Wait -> java.lang.Thread.State: WAITING TIMED_WAITING (on object monitor)
    • Park -> java.lang.Thread.State: WAITING TIMED_WAITING (parking)
    • Monitor -> java.lang.Thread.State: BLOCKED (on object monitor)

    The Park state is a special case of WAITING or TIMED_WAITING. The difference from Wait is that Wait happens on an object monitor (i.e. Object.wait() within a synchronized block). The Park, on the other hand, removes a thread from scheduling via Unsafe.park without any need of holding a monitor (i.e. it doesn't need a synchronized block).