Reputation: 51
Why java reentrant lock does not cause dead lock?
I was studying Reentrant lock, and did realize that a thread failed to try acquire will be placed in a sync queue, and before it parks, it set its prev node's waitStatus to SIGNAL, and recheck if the prev node has set its stat to 0, if not, it might be sure that the prev node has not unparked it, because it has not cleared the SIGNAL state, so it parks. But what if the interval between
shoudParkAFterFailedAcquire and parkAndCheckInterrrupt takes, say 1 second or 1 minute long,
so unpark arrives first before parking. Would this cause a deadlock?
Upvotes: 0
Views: 309
Reputation: 59253
park
and unpark
don't work like you seem to think they do. They are based on a permit-like system:
park
, it blocks until it has a permit and the permit is revoked. If it already has a permit when park
is called, then it doesn't block at all.So it doesn't matter if it takes a minute or more for acquireQueued
to get to the park
, because the preceding call to unpark
has already provided the thread with a permit. The permit will be revoked without blocking.
Upvotes: 4