Optimization 45: Read/Write Locks Another way to ease

October 31, 2006 on 8:34 pm | In Java |

Optimization 45: Read/Write Locks Another way to ease the pain of synchronization is to relax the requirement that one and only one thread may have exclusive access to shared data. The need to synchronize access to shared data stems from the fact that the shared data may be modified by one of the threads accessing it. It follows that we must give exclusive access only to those threads aiming to modify shared data (writers). Conversely, threads that are merely interested in reading shared data (readers) could access shared data concurrently. Reader/writer locks are those that allow multiple readers to access shared data instead of waiting for exclusive access. A thread trying to get read access to shared data will be granted read access in one of two cases: . No other thread was granted access. . The only threads granted access are readers. If a writer thread has been granted access, all readers must wait for the writer thread to leave the critical section. A writer thread is granted access if and only if no other thread has been granted access to the shared resource. Java does not provide built-in read/write synchronization, but you can build your own from the available synchronization primitive building blocks. See D. Lea, Concurrent Programming in Java [LEA97 ], for one such implementation. If all your threads try to modify a shared resource, then reader/writer locks would not help. In fact, they would hurt performance because their implementation is by nature more complex and therefore slower than plain locks. If, however, your shared data is read-mostly, reader/writer locks will improve scalability by eliminating contention among reader threads. Page 174
Note: If you are looking for good and quality webspace to host and run your java application check Actions java hosting services

No Comments yet

TrackBack URI

Sorry, the comment form is closed at this time.