仔细研究了刚发布1.0版本的Google Collections,被其中的MapMaker震惊,这不就是我梦寐以求的Concurrent Map神器吗?如果Google Collection在5年前就发布该有多好?!废话少讲,邀请大家一起来观赏一下什么是MapMaker。
Hashtable太老土啦,线程安全我都用ConcurrentHashMap。什么?现在流行MapMaker?
JDK 1.5引入的ConcurrentHashMap由于其精巧的设计,更高的并发性能,捕获了大家的心,在并发场景中出场率极高,但随着深入的使用,很快的就发现了其中的不足。例如在以Map作为Cache的典型场景中,我们都需要有元素过期的处理,WeakHashMap是这方面的高手,但其在并发方面有点菜(非线程安全),当我们想让这两位大将同时上场的时候,就只能抓耳搔腮了。
Google Collections中的MapMaker融合了Weak Reference,线程安全,高并发性能,异步超时清理,自定义构建元素等强大功能于一身。(注)
常阅读优秀源代码的童鞋都知道,一般叫Maker的对象都是Builder模式,而这个MapMaker就是来"Build"Map的,下面的代码展示了如何构建一个高并发性能,线程安全的WeakHashMap.
public void testWeakKeys() throws Exception {
ConcurrentMap<Key, Value> map = new MapMaker()
.weakKeys() // 指定Map保存的Key为WeakReference机制
.makeMap();
Key key = new Key();
map.put(key, new Value()); // 加入元素
key = null; // key变成了WeakReference
System.gc();// 触发垃圾回收
TimeUnit.SECONDS.sleep(1L);
assertTrue(map.isEmpty()); // map空了,因为WeakReference被回收
}
是不是够简单?他不仅支持WeakKeys,还支持WeakValues。
public void testWeakValues() throws Exception {
ConcurrentMap<Key, Value> map = new MapMaker()
.weakValues() // 指定Map保存的Value为WeakReference机制
.makeMap();
Key key = new Key();
Value value = new Value();
map.put(key, value); // 加入元素
key = null; // Key成了WeakReference
System.gc();// 触发垃圾回收
TimeUnit.SECONDS.sleep(1L);
assertFalse(map.isEmpty()); // map里的东西还在,因为Value还是StrongReference
value = null; // 这次value也变成了WeakReference
System.gc(); // 触发垃圾回收
TimeUnit.SECONDS.sleep(1L);
assertTrue(map.isEmpty()); // map真空了,因为Value是WeakReference被回收
}
还可以选用SoftKeys,和SoftValues,随意组合,比只能WeakKey的WeakHashMap扩展性强太多了。
再来看看On-demand value computation,自定义构建元素。想象下面的场景,你要为一个查询学生信息的DAO增加结果缓存,并且结果超过60秒过期,我们可以用装饰模式结合MapMaker简单的实现。
interface StudentDao {
Information query(String name);
}
class StudentDaoImpl implements StudentDao {
// 真正去查数据库的实现类 代码省略
}
// 装饰器
class CachedStudentDao implements StudentDao {
private final StudentDao studentDao;
private final ConcurrentMap<String, Information> cache;
public CachedStudentDao(final StudentDao studentDao) {
Preconditions.checkNotNull(studentDao, "studentDao");
this.studentDao = studentDao;
this.cache = new MapMaker() // 构建一个 computingMap
.expiration(60, TimeUnit.SECONDS) // 元素60秒过期
.makeComputingMap(new Function<String, Information>(){
@Override
public Information apply(String name) {
return studentDao.query(name);
}
});
// 传入匿名Function自定义缓存的初始化。如果缓存中没有name对应的数据,则调用真正的dao去数据库查找数据,同时缓存结果。
}
@Override
public Information query(String name) {
return cache.get(name); // 从computing cache中取结果
}
}
public void test() {
StudentDao cachedStudentDao = new CachedStudentDao(studentDaoImpl);
// 装饰了studenDaoImpl的cachedStudentDao具备了缓存结果的能力。
}
线程安全,高并发性能,元素过期都实现了,并且代码很简洁。多亏了MapMaker,脏活、累活,就交给它啦。不过要注意的是,要遵循ConcurrentHashMap的规范,其不允许有Null的Key和Value。如果查询出来的结果可能为Null的,可用简单的包装类包装一下,这里不给出代码了。
怎么样?你是不是心动了呢?快下载来看看吧。
注:参考该文章了解是什么WeakReference?
http://www.iteye.com/topic/401478
分享到:
相关推荐
Google_Guava_Collections_使用介绍.pdf )
guava类似Apache Commons工具集包含了若干被Google的 Java项目广泛依赖 的核心库
Guava 是一个 Google 的基于java1.6的类库集合的扩展项目,包括 collections, caching, primitives support, concurrency libraries, common annotations, string processing, I/O, 等等. 这些高质量的 API 可以使你...
Google Guava是一个比较有趣的框架,它提供了很多有趣的的功能, google Guava 给开发者提供了如下常用功能: 集合(collections) 缓存(caching) 原生的类型支持(primitives support) 并发类库(concurrency ...
扩展组件包括guava-annotations、guava-base、guava-bootstrap、guava-collections、guava-concurrent、guava-gwt、guava-io、guava-parent、guava-primitives、listenablefuture、guava-testlib等。
Guava is a set of core Java libraries from Google that includes new collection types (such as multimap and multiset), immutable collections, a graph library, and utilities for concurrency, I/O, ...
Guava工程包含了若干被Google的 Java项目广泛依赖 的核心库,例如:集合 [collections] 、缓存 [caching] 、原生类型支持 [primitives support] 、并发库 [concurrency libraries] 、通用注解 [common annotations] ...
Guava 是一个 Google 的基于java1.6的类库集合的扩展项目,包括 collections, caching, primitives support, concurrency libraries, common annotations, string processing, I/O, 等等. 这些高质量的 API 可以使你...
google-collections-1.0-rc2.jar 的jar包,放心使用。
Guava工程包含了若干被Google的 Java项目广泛依赖 的核心库,例如:集合 [collections] 、缓存 [caching] 、原生类型支持 [primitives support] 、并发库 [concurrency libraries] 、通用注解 [common annotations] ...
Write more robust code that is easier to read and maintain, Learn how to use Preconditions to prevent and find errors faster, Shows how Guava Collections can make working with Java Collections a ...
Guava工程包含了若干被Google的 Java项目广泛依赖 的核心库,例如:集合 [collections] 、缓存 [caching] 、原生类型支持 [primitives support] 、并发库 [concurrency libraries] 、通用注解 [common annotations] ...
Guava 是一个 Google 的基于java1.6的类库集合的扩展项目,包括 collections, caching, primitives support, concurrency libraries, common annotations, string processing, I/O, 等等. 这些高质量的 API 可以使你...
Google Collections是Google公司开发的常用工具库,包括对字符串,文件操作,数据结构和并发程序开发的支持。它比Apache Commons Collections提供了更多更强大的函数,使得程序编写更加简洁,不易产生错误。 这个...
google公共工具类;google collections是google的工程师利用传说中的“20%时间”开发的集合库,它是对java.util的扩展,提供了很多实用的类来简化代码。google collections使用了范型,所以要求jdk1.5以上。
Guava: Google Core Libraries for Java Guava is a set of core libraries that includes new collection types (such as multimap and multiset), immutable collections, a graph library, functional types, ...
oogle collections是google的工程师利用传说中的“20%时间”开发的集合库,它是对java.util的扩展,提供了很多实用的类来简化代码。google collections使用了范型,所以要求jdk1.5以上。它的作者没有像apache ...
赠送jar包:commons-collections4-4.1.jar; 赠送原API文档:commons-collections4-4.1-javadoc.jar; 赠送源代码:commons-collections4-4.1-sources.jar; 赠送Maven依赖信息文件:commons-collections4-4.1.pom;...