当前位置:网站首页>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();
}
}
边栏推荐
猜你喜欢
Programming features of ISP, IAP, ICP, JTAG and SWD
串口通讯继电器-modbus通信上位机调试软件工具项目开发案例
STM32 ADC and DMA
【剑指Offer】42. 栈的压入、弹出序列
Elegant controller layer code
The variables or functions declared in the header file cannot be recognized after importing other people's projects and adding the header file
Trajectory planning for multi-robot systems: Methods and applications 综述阅读笔记
High number_ Chapter 1 space analytic geometry and vector algebra_ Quantity product of vectors
电表远程抄表拉合闸操作命令指令
【acwing】786. Number k
随机推荐
leetcode-304:二维区域和检索 - 矩阵不可变
IIC Basics
Adb 实用命令(网络包、日志、调优相关)
深入分析ERC-4907协议的主要内容,思考此协议对NFT市场流动性意义!
Serial communication relay Modbus communication host computer debugging software tool project development case
Learning records - high precision addition and multiplication
Use the fetch statement to obtain the repetition of the last row of cursor data
STM32 Basics - memory mapping
555电路详解
IDA中常见快捷键
P2788 数学1(math1)- 加减算式
[second on] [jeecgboot] modify paging parameters
[牛客网刷题 Day6] JZ27 二叉树的镜像
This article explains the complex relationship between MCU, arm, muc, DSP, FPGA and embedded system
嵌入式工程师如何提高工作效率
openinstall与虎扑达成合作,挖掘体育文化产业数据价值
BigDecimal数值比较
EasyExcel读取写入简单使用
BUUCTF---Reverse---reverse1
使用U2-Net深层网络实现——证件照生成程序