Answer»
- When you use a synchronized block or method you just need to write synchronized keyword (and provide associated object) acquiring lock and releasing it is done IMPLICITLY.
With ReentrantLock acquiring and releasing lock is done by user using lock() and unlock() methods.
- Synchronized FORCES all lock acquisition and release to occur in a block-structured way which means when multiple locks are ACQUIRED they must be RELEASED in the opposite order, and all locks must be released in the same lexical scope in which they were acquired.
ReentrantLock provides more flexibility, it ALLOWS a lock to be acquired and released in different scopes, and allowing multiple locks to be acquired and released in any order.
- ReentrantLock provides additional functionality over the use of synchronized methods and statements by providing an option for fairness, providing a non-blocking attempt to acquire a lock (tryLock()), an attempt to acquire the lock that can be interrupted (lockInterruptibly(), and an attempt to acquire the lock that can timeout (tryLock(long, TimeUnit)).
With ReentrantLock acquiring and releasing lock is done by user using lock() and unlock() methods.
|