Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding java.lang.Thread.State: WAITING (parking)

First, a really dumb question, I was just wondering what the waiting 'parking' means ? Is the thread waiting to be parked or is it just been parked and therefore is in wait state ? And when that parking happen, how much cpu/memory resources are taken ? What's the purpose of parking a thread ?

Second, by looking at park method in java thread API

Disables the current thread for thread scheduling purposes unless the permit is available.

If the permit is available then it is consumed and the call returns immediately; otherwise the current thread becomes disabled for thread scheduling purposes and lies dormant until one of three things happens.....

English is not my primary language, so I have some difficulties understanding that, I intended 'permit' as kind of 'permission to park the thread', so the questions that follow:

  • what's the meaning of that, what's 'permit', and who and how is checking those permit ?
  • What does that mean: 'if permit is available then it is consumed', is it getting 'parked' ?
  • following, if second point is true, so what's the difference between 'parking' and 'lies dormant' ? If I have permit I can park it forever and if not, I can make it 'dormant' ?

Thanks

like image 520
Leonardo Avatar asked Sep 21 '11 09:09

Leonardo


People also ask

What does it mean when a thread is parked?

When you call a park method on a Thread, it disables the thread for thread scheduling purposes unless the permit is available. You can call unpark method to make available the permit for the given thread, if it was not already available.

What is parking thread in Java?

Method park() disables the current thread for thread scheduling purposes unless the permit is available. unpark(Thread thread) makes available the permit for the given thread, if it was not already available.

What is waiting on condition in thread dump?

Runnable: Means a thread is currently executing. Blocked: The thread is waiting for a lock to be released. This happens when entering a synchronized block, for instance. Waiting / Timed_waiting: The thread is waiting for something to happen.

What is Timed_waiting parking?

TIMED_WAITING The thread is waiting for another thread to perform an action for up to a specified waiting time.


2 Answers

Permit means a permission to continue execution. Parking means suspending execution until permit is available.

Unlike Semaphore's permits, permits of LockSupport are associated with threads (i.e. permit is given to a particular thread) and doesn't accumulate (i.e. there can be only one permit per thread, when thread consumes the permit, it disappears).

You can give permit to a thread by calling unpark(). A thread can suspend its execution until permit is available (or thread is interrupted, or timeout expired, etc) by calling park(). When permit is available, the parked thread consumes it and exits a park() method.

like image 131
axtavt Avatar answered Oct 12 '22 03:10

axtavt


As per the java Thread State Documentation, A thread can go to WAITING state for three reasons:

  1. Object.wait with no timeout
  2. Thread.join with no timeout
  3. LockSupport.park

When you call a park method on a Thread, it disables the thread for thread scheduling purposes unless the permit is available. You can call unpark method to make available the permit for the given thread, if it was not already available.

So, when your Thread is in WAITING mode by LockSupport.park, it will show you as WAITING (parking).

Please make note that, you can call park on current Thread only. This is very helpful mechanism to implement Producer-Consumer Design Pattern.

like image 31
Badal Avatar answered Oct 12 '22 02:10

Badal