2

spring-framework/ConcurrentLruCache.java at main · spring-projects/spring-framework · GitHub

Update

Because there may be multiple readers writing to the queue concurrently, the queue must use a thread-safe data structure, and the put operation of the cache is done in the write lock, can the implementation of the cache be replaced with a HashMap? Because the get method of ConcurrentHashMap is implemented without lock.

Retrieval operations (including get) generally do not block, so may overlap with update operations (including put and remove). Retrievals reflect the results of the most recently completed update operations holding upon their onset. (More formally, an update operation for a given key bears a happens-before relation with any (non-null) retrieval for that key reporting the updated value.)

From ConcurrentHashMap (Java Platform SE 8 )

1 Answer 1

1

Take a closer look at the get method. The choices made to maximize the number of concurrent readers mean the code doesn't enforce the exclusion needed to be able to use non-threadsafe collections.

The cache is accessed without any lock held. The authors want to avoid having to acquire a lock just to read from the cache.

The queue is accessed with a read lock, so there can be multiple concurrent readers. That requires the queue implementation to be threadsafe.

(Of course if the code used a read lock to read from the cache, then the same issue with multiple readers would be true for the cache as well.)

For the question about using hashmap instead of CHM, part of what you get from using locks is memory visibility. If you use HashMap then when the cache is updated the get method also needs to lock in order to avoid all the issues around insufficiently-synchronized code like JIT optimizations and reorderings.

12
  • Now I understand that the queue must be implemented thread-safely, because there may be multiple readers writing to the queue concurrently. But what about cache? Is it possible to use HashMap instead, because only get calls are executed outside the lock.
    – Poison
    Dec 21, 2021 at 3:39
  • @Poison: no, the cache has to be updated, so both the puts and gets would need locking. Dec 21, 2021 at 3:56
  • In the get method of ConcurrentHashMap, it is implemented without lock. If we use HashMap with write lock, can it replace ConcurrentHashMap?
    – Poison
    Dec 21, 2021 at 3:59
  • @Poison: yes but it seems like it would be slower. The linked code reads from the cache to try to avoid taking even a read lock. Dec 21, 2021 at 4:09
  • Sorry, I may not describe clearly. In the operation of ConcurrentLruCache on the cache, the put method is called in the write lock, and only the get method and the contains method are called outside the lock. Does it mean that we can replace the implementation with HashMap?
    – Poison
    Dec 21, 2021 at 4:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.