Wednesday, April 22, 2015

What Does Java Thread State WAITING (parking) In Thread Dump Mean

If you are new to thread dump analysis, you might become confused with this Java thread state WAITING (parking). If a Java thread is in waiting state, that means it is waiting for another thread to call notify on the same object. And if you see too many threads waiting in your thread dump, you will assume there is some issue and that is causing your server to hang. But there is a common case where WAITING (parking) thread state might not be the actual issue. Look at below:
"pool-1-thread-22" prio=5 tid=101b4b000 nid=0x127122000 waiting on condition [127121000]
   java.lang.Thread.State: WAITING (parking)
    at sun.misc.Unsafe.park(Native Method)
    - parking to wait for  <6c340cee0> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
    at java.util.concurrent.locks.LockSupport.park(LockSupport.java:156)
You will see same kind of pool threads with status waiting (parking). These are basically request worker threads on a tomcat sever. These are waiting on a blocking queue until a request comes. When a request comes, one thread will get permit and will run to execute the request.
Same thing will happen when you use your own Java ThreadPoolExecutor. It will create a pool of threads and those threads will wait on a blocking queue provided to the executor. Task executor uses this queue to push and pop tasks as and when they are submitted. So if no tasks are available these threads will wait (park) on the queue. When a task is available, one worker thread will get permit and become a running thread. It will execute the task.
So don't get overwhelmed with waiting (parking) threads. These threads still can cause issue if you have created too many threads in your thread pool and they are eating up your resource. Otherwise you are fine. Your threads are not locked on some object.

No comments:

Post a Comment