use*_*592 8 java concurrency concurrenthashmap
并发Hashmap可以解决在hashmap中看到的同步问题.因此,如果我们使用hashmap同步键工作,添加和删除会很快.如果mulitple线程检查concurrentHashMap大小,那么检查hashmap大小呢?我们还需要同步关键词:如下所示:
public static synchronized getSize(){
return aConcurrentHashmap.size();
}
Run Code Online (Sandbox Code Playgroud)
concurentHashMap.size() 将返回调用时已知的大小,但是当您使用该数字时它可能是一个陈旧的值,因为另一个线程在此期间添加/删除了项目.
但是,ConcurrentMaps的全部目的是您不需要同步它,因为它是一个线程安全的集合.
| 归档时间: |
|
| 查看次数: |
6076 次 |
| 最近记录: |