Java|Java Concurrency
Concurrency
List
List list = new Vector<>();
List list = Collections.synchronizedList(new ArrayList<>());
List list = new CopyOnWriteArrayList<>();
Set
Set set = Collections.synchronizedSet(new HashSet<>());
Set set = new CopyOnWriteArraySet<>();
Map
Map map = Collections.SynchronizedMap(new HashMap<>());
Map map = new ConcurrentHashMap<>();
SynchronizedMap It will maintain the lock at the object level. So if you want to perform any operation like put/get then you have to aquire the lock first. At the same time, other threads are not allowed to perfrom any operaton. So the waiting time will increase here. We can say the performance is relatively low when comparing with ConcurrentHashMap.
ConcurrentHashMap Using finer-grained locking mechanism known as Locking Stripping to allow greater degree of shared access. Due to this it provides better concurrency and scalability.
It will maintain the lock at segment level. It has 16 segments and maintains the concurrency level as 16 by default. So at a time, 16 threads can operate on ConcurrentHashMap. Moreover, read operation doesn’t require a lock, so any number of threads can perform a get operation on it.
Using finer-grained locking mechanism known as Locking Stripping to allow greater degree of shared access. Due to this it provides better concurrency and scalability.
Weak consistency: Iterators returned for ConcurrentHashMap are weakly consistent instead of fail-fast technique used by SynchronizedMap.
Ref 【Java|Java Concurrency】(1) http://blog.sina.com.cn/s/blo...
(2) https://stackoverflow.com/que...
(3) https://www.bilibili.com/read...
推荐阅读
- JAVA(抽象类与接口的区别&重载与重写&内存泄漏)
- 事件代理
- Java|Java OpenCV图像处理之SIFT角点检测详解
- java中如何实现重建二叉树
- 数组常用方法一
- 【Hadoop踩雷】Mac下安装Hadoop3以及Java版本问题
- Java|Java基础——数组
- RxJava|RxJava 在Android项目中的使用(一)
- java之static、static|java之static、static final、final的区别与应用
- Java基础-高级特性-枚举实现状态机