3

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)

gstackoverflow
  • 36,709
  • 117
  • 359
  • 710

2 Answers2

1

park is

sun.misc.Unsafe.park()
Harshal Patil
  • 6,659
  • 8
  • 41
  • 57
Laurent G
  • 397
  • 8
  • 16
1

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).

bashnesnos
  • 816
  • 6
  • 16
  • Still unclear for me. TIMED_WAITING vs WAITING TIMED_WAITING for example. Parking? – gstackoverflow Feb 28 '17 at 17:03
  • You can check that in JavaDoc link I've added. `WAITING` - Thread state for a waiting thread. A thread is in the waiting state due to calling one of the following methods: Object.wait with no timeout, Thread.join with no timeout, LockSupport.park; `TIMED_WAITING` - Thread state for a waiting thread with a specified waiting time. A thread is in the timed waiting state due to calling one of the following methods with a specified positive waiting time: Thread.sleep, Object.wait with timeout, Thread.join with timeout, LockSupport.parkNanos, LockSupport.parkUntil – bashnesnos Mar 01 '17 at 07:23
  • @gstackoverflow I've added clarification for `Park` as well – bashnesnos Mar 01 '17 at 07:36
  • @gstackoverflow it means that the thread won't recieve any CPU time share from the thread scheduler - therefore it won't do anything, until it would be unparked. Here's an overview of that process: http://www.javaworld.com/article/2071214/java-concurrency/java-101--understanding-java-threads--part-3--thread-scheduling-and-wait-notify.html – bashnesnos Mar 01 '17 at 08:01