Java Concurrency Interview

Locks, conditions and AQS question 41 of 180 senior

What is LockSupport.park, and why is it better than suspend/resume?

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.

locks internals

Quiz yourself on Locks, conditions and AQS Suggest a correction