当前位置:网站首页>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();
}
}
边栏推荐
- Why is the reflection efficiency low?
- Use the fetch statement to obtain the repetition of the last row of cursor data
- 深入理解Apache Hudi异步索引机制
- Socket通信原理和实践
- Experience sharing of software designers preparing for exams
- How embedded engineers improve work efficiency
- Elegant controller layer code
- [higherhrnet] higherhrnet detailed heat map regression code of higherhrnet
- Programming features of ISP, IAP, ICP, JTAG and SWD
- Leetcode-304: two dimensional area and retrieval - matrix immutable
猜你喜欢
Trajectory planning for multi robot systems: methods and Applications Overview reading notes
Trajectory planning for multi-robot systems: Methods and applications 综述阅读笔记
Prototype object in ES6
Inno setup packaging and signing Guide
1323:【例6.5】活动选择
Some properties of leetcode139 Yang Hui triangle
P1031 [NOIP2002 提高组] 均分纸牌
Leetcode-560: subarray with sum K
Serial communication relay Modbus communication host computer debugging software tool project development case
STM32 Basics - memory mapping
随机推荐
2022.7.3DAY595
Five simple and practical daily development functions of chrome are explained in detail. Unlock quickly to improve your efficiency!
路由器开发知识汇总
Study summary of postgraduate entrance examination in November
那些易混淆的概念(三):function和class
openinstall与虎扑达成合作,挖掘体育文化产业数据价值
【acwing】789. Range of numbers (binary basis)
Leetcode-304: two dimensional area and retrieval - matrix immutable
Study summary of postgraduate entrance examination in July
求最大公约数与最小公倍数(C语言)
MCU is the most popular science (ten thousand words summary, worth collecting)
Hdu-2196 tree DP learning notes
@Configuration, use, principle and precautions of transmission:
How embedded engineers improve work efficiency
【剑指Offer】42. 栈的压入、弹出序列
IPv4 socket address structure
Why is the reflection efficiency low?
Weekly recommended short videos: what are the functions of L2 that we often use in daily life?
Easyexcel read write simple to use
ThreadLocal会用可不够