Rnet
Rnet

Reputation: 5040

Java parked thread

What is the difference between a parked thread and a waiting thread in java ? I've a jboss core dump and analysing it is showing a lot of parked threads.

Upvotes: 33

Views: 26956

Answers (4)

Kaan
Kaan

Reputation: 5754

Both park() and wait() will result in a disabled thread. Making a disabled thread active again depends on how it was disabled.

A thread that has been disabled by calling LockSupport.park() will remain disabled until:

  • some other thread calls unpark(), or
  • some other thread calls interrupt(), or
  • "the call spuriously (that is, for no reason) returns"

A thread that has been disabled by calling Object's wait() – which is equivalent to calling wait(0) – will remain disabled until:

Upvotes: 10

herseus
herseus

Reputation: 61

In Java, a parked thread by calling LockSupport.park() method is a waiting thread ( in the Thread.state.WAITING ).

See the Java Doc for Thread.state.WAITING.

There are 3 ways to cause a thread to be in the WAITING status:

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

A thread in the waiting state is waiting for another thread to perform a particular action.

For example, a thread that has called Object.wait() on an object is waiting for another thread to call Object.notify() or Object.notifyAll() on that object. A thread that has called Thread.join() is waiting for a specified thread to terminate.

Upvotes: 6

dash1e
dash1e

Reputation: 7807

Look at Javadoc the park() method:

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:

Some other thread invokes unpark with the current thread as the target; or Some other thread interrupts the current thread; or The call spuriously (that is, for no reason) returns. This method does not report which of these caused the method to return. Callers should re-check the conditions which caused the thread to park in the first place. Callers may also determine, for example, the interrupt status of the thread upon return.

So a parked thread is a thread blocked using LockSupport.park().

Upvotes: 25

vmishra
vmishra

Reputation: 1762

LockSupport - class takes facility basic thread blocking primitives for creating locks and other synchronization classes.

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.

Use it like:
finally { LockSupport.unpark(thread); }

Upvotes: -2

Related Questions