先看putIfAbsent 这个方法
线程安全的情况修改值
ConcurrentHashMap的putIfAbsent
这个方法在key不存在的时候加入一个值,如果key存在就不放入,等价:
if (!map.containsKey(key))
return map.put(key, value);
else
return map.get(key);
测试代码:
复制代码
public class Test {
public static void main(String[] args) {
ConcurrentHashMap<String,String> map=new ConcurrentHashMap<String,String>();
String temp=map.putIfAbsent("a", "gaoxing");
System.out.println(map.get("a"));
temp=map.putIfAbsent("a","nihao");
System.out.println(map.get("a"));
temp=map.putIfAbsent("a","gaoxing");
System.out.println(map.get("a"));
}
}
复制代码
结果为
gaoxing
gaoxing
gaoxing
ConcurrentHashMap的putIfAbsent用来做缓冲相当不错,多线程安全的
在看LevelCache 存储了ConcurrentHashMap 这个 对象干什么用呢?
level 是一个单例 只是用来存储StorageLevel 的值,
本来可以存一个16位 int类型
前8位存储标准位 后8为存储份数
足够
private[spark] val storageLevelCache = new ConcurrentHashMap[StorageLevel, StorageLevel]()
private[spark] def getCachedStorageLevel(level: StorageLevel): StorageLevel = {
storageLevelCache.putIfAbsent(level, level)
storageLevelCache.get(level)
}
设计太奇葩了 在hadoop 存储目录权限也是用shortint存储
线程安全的情况修改值
ConcurrentHashMap的putIfAbsent
这个方法在key不存在的时候加入一个值,如果key存在就不放入,等价:
if (!map.containsKey(key))
return map.put(key, value);
else
return map.get(key);
测试代码:
复制代码
public class Test {
public static void main(String[] args) {
ConcurrentHashMap<String,String> map=new ConcurrentHashMap<String,String>();
String temp=map.putIfAbsent("a", "gaoxing");
System.out.println(map.get("a"));
temp=map.putIfAbsent("a","nihao");
System.out.println(map.get("a"));
temp=map.putIfAbsent("a","gaoxing");
System.out.println(map.get("a"));
}
}
复制代码
结果为
gaoxing
gaoxing
gaoxing
ConcurrentHashMap的putIfAbsent用来做缓冲相当不错,多线程安全的
在看LevelCache 存储了ConcurrentHashMap 这个 对象干什么用呢?
level 是一个单例 只是用来存储StorageLevel 的值,
本来可以存一个16位 int类型
前8位存储标准位 后8为存储份数
足够
private[spark] val storageLevelCache = new ConcurrentHashMap[StorageLevel, StorageLevel]()
private[spark] def getCachedStorageLevel(level: StorageLevel): StorageLevel = {
storageLevelCache.putIfAbsent(level, level)
storageLevelCache.get(level)
}
设计太奇葩了 在hadoop 存储目录权限也是用shortint存储