InterviewSolution
| 1. |
How Readwritelock Can Help In Reducing Contention Among Multiple Threads? Or What Is Benefit Of Using Readwritelock In Java? |
|
Answer» ReadWriteLock PROVIDES separate set of locks for reading and writing operations. Where read lock may be held simultaneously by multiple READER threads, so long as there are no writers. The write lock is exclusive. So read operations are not mutually exclusive. It exploits the fact that while only a single thread at a time (a writer thread) can MODIFY the shared data, in many cases any NUMBER of threads can concurrently read the data (hence reader threads). Thus in the applications where reads are more than writes or duration of reads is more the thread contention will be less as read lock is shared by many thread rather than being mutually exclusive. So you won't have a situation where only one thread is reading and other threads are waiting. ReadWriteLock provides separate set of locks for reading and writing operations. Where read lock may be held simultaneously by multiple reader threads, so long as there are no writers. The write lock is exclusive. So read operations are not mutually exclusive. It exploits the fact that while only a single thread at a time (a writer thread) can modify the shared data, in many cases any number of threads can concurrently read the data (hence reader threads). Thus in the applications where reads are more than writes or duration of reads is more the thread contention will be less as read lock is shared by many thread rather than being mutually exclusive. So you won't have a situation where only one thread is reading and other threads are waiting. |
|