A deadlock is a condition where two or more threads are waiting for a lock promotion on the same object due to the other threads holding a shared lock. Specifically, this happens when two threads hold an S lock on an object, and both threads then request an X lock to write changes to the object. The general case is that two threads attempt to simultaneously update a shared object resulting in lock contention because neither thread can obtain the higher-level lock it needs to write the changes.
The different locking strategies deal with this situation with some help from the developer. The optimistic locking strategy holds locks for very brief time periods. A lock is only held as long as it takes a thread to read or write an object. When thread 1 calls a get
method for a payment with a key of 5000, the S lock is obtained for key 5000. After obtaining the lock, our payments BackingMap returns the object with that key and releases the S lock. This ensures that only objects...