dugenkui03 opened a new issue #2610: 优化 LRUCache读取性能 URL: https://github.com/apache/incubator-dubbo/issues/2610 [LRUCache](https://github.com/apache/incubator-dubbo/blob/master/dubbo-common/src/main/java/org/apache/dubbo/common/utils/LRUCache.java) 问题 查询可以加共享锁,更新时才需要加排他锁。因此可以使用信号量替代排他锁提高查询操作的性能。 思路: 1. 必须使用公平锁:由AQS和Semaphore源码可知非公平锁在高并发情况下会导致更新操作难以执行; 2. 信号量初始值为Integer.MAX_VALUE,读取操作则将状态值减1,更新操作则将状态值减Integer.MAX_VALUE,以此来实现查询操作加共享锁,更新操作加排他锁。 主要修改: ```java //1. 排他锁ReentrantLock替换成信号来能Semaphore; //2.size()->super.size()父类方法不需要获取许可,且仅在put类操作中被调用 @Override protected boolean removeEldestEntry(java.util.Map.Entry<K, V> eldest) { return super.size() > maxCapacity; } ``` ##### 参考: - 《java并发编程实战》14.5 和 14.6 小节 - [信号量测试](https://github.com/dugenkui03/java-source-code/commit/740ac89a43896337a9b09b82775ebc1947684f1e)
---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
