支持并发的结果缓存
来源:互联网 发布:淘宝网阿里巴巴找密码 编辑:程序博客网 时间:2024/05/19 16:34
改自《Java Concurrency in Practice》Listing 5.19. Final Implementation of Memoizer.
比我之前自己写的好多了,必备工具类,记下来留着以后常用
特点:
1.使用Future与ConcurrentMap#putIfAbsent(K key, V value)避免了多个线程重复计算
2.如果计算被取消或失败,会调用ConcurrentMap#remove(key, future)来避免Cache Pollution
使用举例:
比我之前自己写的好多了,必备工具类,记下来留着以后常用
特点:
1.使用Future与ConcurrentMap#putIfAbsent(K key, V value)避免了多个线程重复计算
2.如果计算被取消或失败,会调用ConcurrentMap#remove(key, future)来避免Cache Pollution
- public interface Computable<K, V> {
- V compute(final K key) throws InterruptedException;
- }
- public class ConcurrentCache<K, V> implements Computable<K, V> {
- private static final int DEFAULT_INITIAL_CAPACITY = 16;
- private static final int DEFAULT_CONCURRENCY_LEVEL = 16;
- private static final float DEFAULT_LOAD_FACTOR = 0.75f;
- private final ConcurrentMap<K, Future<V>> cache;
- private final Computable<K, V> computable;
- public ConcurrentCache(Computable<K, V> computable) {
- this(computable, DEFAULT_INITIAL_CAPACITY, DEFAULT_CONCURRENCY_LEVEL);
- }
- public ConcurrentCache(Computable<K, V> computable, int initialCapacity, int concurrencyLevel) {
- this(computable, initialCapacity, DEFAULT_LOAD_FACTOR, concurrencyLevel);
- }
- public ConcurrentCache(Computable<K, V> computable, int initialCapacity, float loadFactor, int concurrencyLevel) {
- this.computable = computable;
- cache = new ConcurrentHashMap<K, Future<V>>(initialCapacity, loadFactor, concurrencyLevel);
- }
- @Override
- public V compute(final K key) throws InterruptedException {
- while (true) {
- Future<V> future = cache.get(key);
- if (future == null) {
- FutureTask<V> futureTask = new FutureTask<V>(new Callable<V>() {
- @Override
- public V call() throws Exception {
- return computable.compute(key);
- }
- });
- /**
- * V putIfAbsent(K key, V value)
- * 等效于以下代码,只不过以原子操作的方式执行
- * if (!map.containsKey(key))
- * return map.put(key, value);
- * else
- * return map.get(key);
- */
- future = cache.putIfAbsent(key, futureTask);
- if (future == null) {
- future = futureTask;
- futureTask.run();
- }
- }
- try {
- return future.get();
- } catch (InterruptedException e) {
- /**
- * boolean remove(Object key, Object value)
- * 等效于以下代码,只不过以原子操作的方式执行
- * if (map.containsKey(key) && map.get(key).equals(value)) {
- * map.remove(key);
- * return true;
- * } else {
- * return false;
- * }
- */
- cache.remove(key, future);
- throw e;
- } catch (CancellationException e) {
- cache.remove(key, future);
- } catch (ExecutionException e) {
- cache.remove(key, future);
- throw new RuntimeException(e.getCause());
- } catch (RuntimeException e) {
- cache.remove(key, future);
- throw e;
- }
- }
- }
- }
使用举例:
- static final ConcurrentCache<Class<?>, Map<String, FieldInfo>> FIELDINFO_GETTERS_CACHE =
- new ConcurrentCache<Class<?>, Map<String, FieldInfo>>(
- new Computable<Class<?>, Map<String, FieldInfo>>() {
- @Override
- public Map<String, FieldInfo> compute(Class<?> clazz) throws InterruptedException {
- Map<String, FieldInfo> fieldInfoMap = new HashMap<String, FieldInfo>();
- // 省略...
- return fieldInfoMap;
- }
- });
- public static Map<String, FieldInfo> computeGetters(final Class<? extends Event> clazz) {
- return FIELDINFO_GETTERS_CACHE.compute(clazz);
- }
0 0
- 支持并发的结果缓存
- 支持并发的结果缓存
- java并发编程--构造高效的结果缓存
- 构建高效且可伸缩的结果缓存引申的并发测试规范化
- Java并发(具体实例)——构建高效且可伸缩的结果缓存
- java并发——构建高效且可伸缩的结果缓存
- java并发编程实战-构建高效且可伸缩的结果缓存
- Ehcache的并发支持
- 构建高效的结果缓存
- struts2 支持的结果类型
- Struts2支持的结果类型
- Struts2支持的结果类型
- tomcat支持的并发数
- Ehcache对并发的支持
- 003-支持并发的饿汉单例
- 支持缓存的autocomplete组件
- SpringBoot的数据缓存支持
- 关于缓存穿透、缓存并发、缓存失效的解决方案
- Java IO机制
- linux下创建和删除软、硬链接
- crf
- 从set/map谈到hashtable/hash_map/hash_set
- 内存碎片
- 支持并发的结果缓存
- 用sed -i修改文件,去掉 “行首” 带“@”的首字母@
- 《ODAY安全:软件漏洞分析技术》学习心得-----shellcode的一点小小的思考
- UVA - 299 Train Swapping
- POJ 1125 Stockbroker Grapevine(多源求最短路)
- DataGridView数据绑定到支持更改通知并允许删除的IBindinglist,否则不能以编程的方式移除
- Objective-C底层数据结构
- 对find参数-prune的理解
- Flume日志收集