sychronized versus ReentrantLock

比较synchronized和ReentrantLock


1. synchronized

When a thread requests a lock that is already held by another thread, the requesting
thread blocks. But because intrinsic locks are reentrant, if a thread tries
to acquire a lock that it already holds, the request succeeds. Reentrancy means
that locks are acquired on a per-thread rather than per-invocation basis.7 Reentrancy
is implemented by associating with each lock an acquisition count and an
owning thread. When the count is zero, the lock is considered unheld. When a
thread acquires a previously unheld lock, the JVM records the owner and sets the
acquisition count to one. If that same thread acquires the lock again, the count

  1. This differs from the default locking behavior for pthreads (POSIX threads) mutexes, which are
    granted on a per-invocation basis.
    2.4. Guarding state with locks 27
    is incremented, and when the owning thread exits the synchronized block, the
    count is decremented. When the count reaches zero, the lock is released.