Java Reference
In-Depth Information
Like ReentrantLock , thewrite lock in ReentrantReadWriteLock has a unique
owner and can be released only by the thread that acquired it. In Java 5.0, the read lock be-
haves more like a Semaphore than a lock, maintaining only the count of active readers, not
their identities. This behavior was changed in Java 6 to keep track also of which threads have
been granted the read lock. [6]
Read-write locks can improve concurrency when locks are typically held for a moderately
long time and most operations do not modify the guarded resources. ReadWriteMap in
Listing 13.7 uses a ReentrantReadWriteLock to wrap a Map so that it can be shared
safely by multiple readers and still prevent reader-writer or writer-writer conflicts. [7] In real-
ity, ConcurrentHashMap 's performance is so good that you would probably use it rather
than this approach if all you needed was a concurrent hash-based map, but this technique
would be useful if you want to provide more concurrent access to an alternate Map imple-
mentation such as LinkedHashMap .
Search WWH ::




Custom Search