ConcurrentMap
2016-11-01 23:45
61 查看
java.util.concurrent.ConcurrentMap
The java.util.concurrent.ConcurrentMapinterface represents a Map which
is capable of handling concurrent access (puts and gets) to it.
The
ConcurrentMaphas a few extra atomic methods in addition to the methods it inherits from its superinterface,
java.util.Map.
ConcurrentMap Implementations
Since ConcurrentMapis an interface, you need to use one of its implementations in order to use it. The
java.util.concurrentpackage contains the following implementations of the
ConcurrentMapinterface:
ConcurrentHashMap
ConcurrentHashMap
TheConcurrentHashMapis very similar to the
java.util.HashTableclass, except that
ConcurrentHashMapoffers better concurrency than
HashTabledoes.
ConcurrentHashMapdoes
not lock the
Mapwhile you are reading from it. Additionally,
ConcurrentHashMapdoes not lock the entire
Mapwhen writing to it. It only locks the part
of the
Mapthat is being written to, internally.
Another difference is that
ConcurrentHashMapdoes not throw
ConcurrentModificationExceptionif the
ConcurrentHashMapis changed while being iterated.
The
Iteratoris not designed to be used by more than one thread though.
Checkout the official JavaDoc for more details about
ConcurrentMapand
ConcurrentHashMap.
ConcurrentMap Example
Here is an example of how to use the ConcurrentMapinterface. The example uses a
ConcurrentHashMapimplementation:
ConcurrentMap concurrentMap = new ConcurrentHashMap(); concurrentMap.put("key", "value"); Object value = concurrentMap.get("key");
相关文章推荐
- 《深入浅出 Java Concurrency》—并发容器 ConcurrentMap
- Map 和ConcurrentMap 线程不安全和线程安全证明
- Java 8并发教程:原子变量和ConcurrentMap
- ConcurrentMap、hashTable与hashMap的区别
- HashMap和Hashtable和HashTree和ConcurrentMap的区别
- Java ConcurrentMap 源码分析
- ConcurrentMap 并发容器
- 为什么ConcurrentMap不允许null
- 【转】ConcurrentMap 分析和思考
- HashMap HashTable ConcurrentMap
- ConcurrentMap、hashTable与hashMap的区别
- ConcurrentMap
- 深入浅出 Java Concurrency (16): 并发容器 part 1 ConcurrentMap (1)
- 并发容器 ConcurrentMap
- Spring Cache抽象-基于XML的配置声明(基于ConcurrentMap的配置)
- 《java.util.concurrent 包源码阅读》04 ConcurrentMap
- Java并发包:ConcurrentMap
- Java常见集合框架(二十一): Map之ConcurrentMap、ConcurrentHashMap
- ConcurrentMap和HashMap的区别
- Java集合源码学习(24)_ConcurrentMap的实现类ConcurrentHashMap