What is Lock splitting technique in Java? Which technique is used in ReadWriteLock class for reducing Lock contention in Java? Java interview
What is Lock splitting technique?
Lock splitting is a technique to reduce Lock contention in multithreading.
It is applicable in scenario when one lock is used to synchronize access to different aspects of the same application.
Sometimes we put one lock to protect the whole array. There can be multiple threads trying to get the lock for same array.
This single lock on array can cause Lock contention among threads.
To resolve this we can give one lock to each element of the array. Or we can use modulus function to assign different locks to a small group of array elements.
In this way we can reduced the chance of Lock contention. This is Lock splitting technique.
Which technique is used in ReadWriteLock class for reducing Lock contention?
ReadWriteLock uses two locks. One lock for read-only operations, another lock for write operations.
Its implementation is based on the premise that concurrent threads do not need a lock when they want to read a value while no otherthread is trying to write.
In this implementation, read-only lock can be obtained by multiple threads.
And the implementation guarantees that all read operation will see only the latest updated value as soon as the write lock is released.
Comments
Post a Comment