Spring ConcurrentReferenceHashMap简单说明
来源:互联网 发布:python datetime 加减 编辑:程序博客网 时间:2024/06/15 20:28
Spring ConcurrentReferenceHashMap简单测试
ConcurrentReferenceHashMap是自spring3.2后增加的一个同步的软(虚)引用Map。关于软引用(SoftRefrence)和虚引用(WeakRefrence)可以参见java四种引用类型。废话不多说直接上测试代码:
@Testpublic void test() throws InterruptedException { String key = new String("key"); String value = new String("val"); Map<String, String> map = new ConcurrentReferenceHashMap<>(8, ReferenceType.WEAK); map.put(key, value); System.out.println(map); key = null; System.gc(); TimeUnit.SECONDS.sleep(5); System.out.println(map);}
输出结果:
神奇的事发生了。通过代码我们可以看到。我先构建了一个虚引用的map对象(也就是本文主角ConcurrentReferenceHashMap),然后新建对象key,value并将两个对象放入Map中进行保存。然后使key对象的强引用置为null。然后调用系统GC。由于系统GC的特殊性质并不能保证系统立马进行GC操作所已紧接着让主线程睡眠5s。接着打印我们的map对象发现map中的对象自动被移除了。
接下来我不置空key而将value置空发现结果相同。
结论:
查看ConcurrentReferenceHashMap源码发现起底层实现依赖的是RefrenceQueue完成自动移除操作。时间有限就写到这里。有时间再进行完善。
0 0
- Spring ConcurrentReferenceHashMap简单说明
- Spring ConcurrentReferenceHashMap简单测试
- Spring—ConcurrentReferenceHashMap
- spring aop expression简单说明
- spring aop expression简单说明
- spring aop expression简单说明
- spring aop expression简单说明
- spring aop expression简单说明
- spring aop expression简单说明
- spring aop expression简单说明
- spring aop expression简单说明
- Spring aop expression 简单说明
- Spring 3 各JAR包简单说明
- Spring Task 应用的简单说明
- spring aop @aspectj的简单说明
- spring的线程池简单说明
- spring WEB MVC + Spring + hibernate开发web程序的配置说明和简单实例的详细说明
- 关于使用Spring和hibernate开发web程序的配置说明和简单实例的详细说明
- 未能加载文件或程序集 newtonsoft.json,version=4.5.0.0
- 8大爱的方式让您的婚礼绿色环保
- JAVA特殊API
- linkbutton按钮组件
- NoClassDefFoundError Eclipse 引用jar包 运行报错
- Spring ConcurrentReferenceHashMap简单说明
- XToDo的使用
- 不同控制器间model传递
- ESP32 beta 测试申请成功
- STM32 BOOT 引脚配置的说明
- 商品详情页系统的Servlet3异步化实践
- WINDOWS 中 UNICODE的转换
- 开发工具网站
- 华为机试——数组循环移位