ConcurrentHashMap融合了Hashtable和HashMap二者的优势。
Hashtable是做了线程同步,HashMap未考虑同步。所以HashMap在单线程下效率较高,Hashtable在多线程下同步操作能保证程序的正确性。 但是Hashtable每次执行同步操作都需要锁住整个结构。
ConcurrentHashMap的出现就是为了解决Hashtable同步lock整个数据结构的问题。ConcurrentHashMap锁的方式是细颗粒度。
ConcurrentHashMap将Hash表分为16个桶(默认值),诸如get/put/remove操作只需要锁着需要的单个桶即可。
ConcurrentHashMap只有在size等操作的时候才会锁住整个Hash表。
下面是自己实现的一个ConcurrentHashMap的本地缓存的例子:ConcurrentHashMap 和Guava cache相比,需要自己显示的删除缓存
import java.util.concurrent.ConcurrentHashMap;
public class ConcurrentHashMapTest {
private static ConcurrentHashMap<String, String> cacheMap = new ConcurrentHashMap<>();
/**
* 获取缓存的对象
*
* @param account
* @return
*/
public static String getCache(String account) {
account = getCacheKey(account);
// 如果缓冲中有该账号,则返回value
if (cacheMap.containsKey(account)) {
return cacheMap.get(account);
}
// 如果缓存中没有该账号,把该帐号对象缓存到concurrentHashMap中
initCache(account);
return cacheMap.get(account);
}
/**
* 初始化缓存
*
* @param account
*/
private static void initCache(String account) {
// 一般是进行数据库查询,将查询的结果进行缓存
cacheMap.put(account, "18013093863");
}
/**
* 拼接一个缓存key
*
* @param account
* @return
*/
private static String getCacheKey(String account) {
return Thread.currentThread().getId() + "-" + account;
}
/**
* 移除缓存信息
*
* @param account
*/
public static void removeCache(String account) {
cacheMap.remove(getCacheKey(account));
}
}
package com.zsplat.yyzx.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 缓存机制
*/
public class CacheUtil {
private static CacheUtil cacheUtil;
private static Map<String,Object> cacheMap;
private CacheUtil(){
cacheMap = new HashMap<String, Object>();
}
public static CacheUtil getInstance(){
if (cacheUtil == null){
cacheUtil = new CacheUtil();
}
return cacheUtil;
}
/**
* 添加缓存
* @param key
* @param obj
*/
public void addCacheData(String key,Object obj){
cacheMap.put(key,obj);
}
/**
* 取出缓存
* @param key
* @return
*/
public Object getCacheData(String key){
return cacheMap.get(key);
}
/**
* 清楚缓存
* @param key
*/
public void removeCacheData(String key){
cacheMap.remove(key);
}
}
package com.zhegui.utils.cache;
public class CacheClientTest {
public static void main(String[] args) throws InterruptedException {
ConcurrentHashMapCache cache = ConcurrentHashMapCache.getInstance();
String value = "test3333";
String key = "key1";
cache.put(key, value);
String key2 = "key2";
String value2 = "value222";
cache.put(key2, value2, 30 * 1000);
String key3 = "key3";
String value3 = "value33333333";
cache.put(key3, value3, 2 * 60 * 1000);
Thread.sleep(60 * 1000);
System.out.println("key1: value = "+cache.get(key));
System.out.println("key2: value2 = " + cache.get(key2));
System.out.println("key3: value3 = " + cache.get(key3));
System.out.println("-------remove key1 --------");
cache.remove(key);
System.out.println("key1: value = "+cache.get(key));
System.out.println("key2: value2 = " + cache.get(key2));
System.out.println("key3: value3 = " + cache.get(key3));
Thread.sleep(90 * 1000);
System.out.println("key3: value3 = " + cache.get(key3));
}
}
版权声明:本文为qq_32521313原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。