当前位置:网站首页>ArrayList thread insecurity and Solutions
ArrayList thread insecurity and Solutions
2022-07-07 10:30:00 【HGW689】
What we usually say Java Medium fail-fast Mechanism ( Fast failure mechanism ), Default means Java An error detection mechanism in a collection , When multiple threads make structural changes to the collection , It's possible to start fail-fast Mechanism , This time it will throw ConcurrentModificationException abnormal .
Why? ArrayList Thread unsafe ?Vector But thread safety ? Let's explore how !
ArrayList:
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
//ensureCapacityInternal() Is to judge if the current new element is added to the list , List elementData Whether the size of the array meets , If size + 1 The length of this requirement is greater than elementData The length of this array , Then we need to expand the capacity of this array
elementData[size++] = e;
return true;
}
Vector:
public synchronized boolean add(E e) {
modCount++;
ensureCapacityHelper(elementCount + 1);
elementData[elementCount++] = e;
return true;
}
Obvious : stay ArrayList in ,elementData And size It's all global variables , But it didn't happen sychronization Synchronous processing ,elementData Shared threads are unsafe mutable Variable data .
So how to solve LIst What about thread safety ?
- Vector:Vector list = new Vector<>();
- Collections:List list = Collections.synchronizedList(new ArrayList());
- CopyOnWriteArrayList:List list = new CopyOnWriteArrayList<>();
Let's see CopyOnWriteArrayList Medium add() Method , The principle is to use Copy on write technology :
public boolean add(E e) {
final ReentrantLock lock = this.lock;
// First : Lock !
lock.lock();
try {
Object[] elements = getArray();
int len = elements.length;
// 1、 Copy the content
Object[] newElements = Arrays.copyOf(elements, len + 1);
// 2、 Write new content
newElements[len] = e;
// 3、 Merge
setArray(newElements);
return true;
} finally {
lock.unlock();
}
}
边栏推荐
- How embedded engineers improve work efficiency
- 南航 PA3.1
- TypeScript 接口继承
- 施努卡:机器视觉定位技术 机器视觉定位原理
- Study summary of postgraduate entrance examination in August
- 关于hzero-resource报错(groovy.lang.MissingPropertyException: No such property: weight for class)
- Some properties of leetcode139 Yang Hui triangle
- 优雅的 Controller 层代码
- 施努卡:机器人视觉抓取工作原理 机器视觉抓取
- leetcode-560:和为 K 的子数组
猜你喜欢

Elegant controller layer code

Pre knowledge reserve of TS type gymnastics to become an excellent TS gymnastics master

ArrayList线程不安全和解决方案

555电路详解

Appx代码签名指南
![[higherhrnet] higherhrnet detailed heat map regression code of higherhrnet](/img/3c/890f2481231482645554f86dd614a9.png)
[higherhrnet] higherhrnet detailed heat map regression code of higherhrnet

The variables or functions declared in the header file cannot be recognized after importing other people's projects and adding the header file

JMeter about setting thread group and time
![P1031 [NOIP2002 提高组] 均分纸牌](/img/ba/6303f54d652fa7aa89440e314f8718.png)
P1031 [NOIP2002 提高组] 均分纸牌

【HigherHRNet】 HigherHRNet 详解之 HigherHRNet的热图回归代码
随机推荐
Factorial implementation of large integer classes
1324:【例6.6】整数区间
TypeScript 接口继承
openinstall与虎扑达成合作,挖掘体育文化产业数据价值
2022.7.5DAY597
Prototype and prototype chain
IPv4 socket address structure
1321:【例6.3】删数问题(Noip1994)
OpenGL glLightfv 函数的应用以及光源的相关知识
Some properties of leetcode139 Yang Hui triangle
Leetcode exercise - 113 Path sum II
成为优秀的TS体操高手 之 TS 类型体操前置知识储备
Differences between MCU and MPU
IO模型复习
[daiy5] jz77 print binary tree in zigzag order
基于HPC场景的集群任务调度系统LSF/SGE/Slurm/PBS
[email protected] can help us get the log object quickly
关于easyflash v3.3使用过程的记录
Inno setup packaging and signing Guide
P2788 数学1(math1)- 加减算式