简体   繁体   English

为什么 Spring 中的 ConcurrentLruCache 即使使用了读写锁,仍然使用线程安全的映射和队列,而不是普通的映射和队列?

[英]Why does ConcurrentLruCache in Spring still use thread-safe maps and queues instead of ordinary maps and queues even when read-write lock are used?

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

Take a closer look at the get method.仔细看看 get 方法。 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.为最大化并发阅读器数量所做的选择意味着代码不会强制执行能够使用非线程安全 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.) (当然,如果代码使用读锁从缓存中读取,那么多个读取器的相同问题也适用于缓存。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM