Locks, conditions and AQS question 41 of 180 senior
What is LockSupport.park, and why is it better than suspend/resume?
- It is the same thing with a new name
- Correct: It is permit-based, so an
unparkthat arrives before theparkis remembered and the park returns immediately, which removes the lost-wakeup race - It never blocks
- It only works inside
synchronized
park() and unpark(thread) are the primitives AQS blocks on. Each thread has a single permit. An
unpark makes the permit available, and a park consumes it, blocking only if none is there. Because
the permit is remembered, ordering does not matter, which is exactly the race that made the deprecated
suspend/resume pair unusable: a resume arriving first was lost and the thread suspended forever.
Two details. park may return spuriously, so callers loop, the same rule as wait. And a parked
thread appears as WAITING in a dump with LockSupport.park at the top of the stack, which is what
every java.util.concurrent blocking operation looks like.
Quiz yourself on Locks, conditions and AQS Suggest a correction