Transforming the example using ReentrantLocks would indeed mean using two locks: one associated with d1
and the other one associated with d2
.
And you would replace every entrance in a synchronized block on dX
by a call to lockX.lock()
, and any exit from a synchronized block on dX
by a call to lockX.unlock()`.
Using tryLock()
defeats the purpose, since it returns instead of waiting if the lock can't be acquired.