Java / Java Multithreading
Why do we use ReentrantLock over synchronized (this)?
A ReentrantLock is unstructured and flexible compared to the synchronized constructs. We don't need to use a block structure for locking and can even hold a lock across methods.
private ReentrantLock lock; public void method1() { ... lock.lock(); ... } public void method2() { ... lock.unlock(); ... }
Also ReentrantLock supports lock polling and interruptible lock waits that supports time-out. ReentrantLock also has support for configurable fairness policy, allowing more flexible thread scheduling.
More Related questions...