opencms7.5

[/code]// 系统编码
String systemEncoding = (new OutputStreamWriter(new ByteArrayOutputStream())).getEncoding();

HashMap vs FastHashMap:原文参考http://frenchmay.iteye.com/blog/226784
[code="java"]
public class FastHashMap extends HashMap {
protected HashMap map = null;
protected boolean fast = false;
public FastHashMap() {
super();
this.map = new HashMap();
}
}
FastHashMap继承于HashMap,存储由HashMap实现的,通过fast来设置是否是以快速模式执行.可以初步认为FashHashMap是HashMap的一个代理.



public Object get(Object key) {
if (fast) {
// get方法的实现是线程安全的
return (map.get(key));
} else {
// 对整个map对象同步
synchronized (map) {
return (map.get(key));
}
}
}
所以FastHashMap在get速度上比HashMap快!



public Object put(Object key, Object value) {
if (fast) {
synchronized (this) {
// 克隆一个map,再在map上操作,再替换map;在替换map前所有对象都读取map;
HashMap temp = (HashMap) map.clone();
Object result = temp.put(key, value);
map = temp;
return (result);
}
} else {
synchronized (map) {
return (map.put(key, value));
}
}
}


做的测试是在slow模式下的FastHashMap和hashmap在单线程访问的情形下,读/取50000
个long类型的测试举例.


结果是

Result:

HashMap put 50000 Object using1.021s

FastHashMap put 50000 Object using1.221s

HashMap get 50000 Object using0.561s

FastHashMap get 50000 Object using0.04s

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章