当前位置:网站首页>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();
}
}
边栏推荐
- leetcode-303:区域和检索 - 数组不可变
- Guid primary key
- 0x0fa23729 (vcruntime140d.dll) (in classes and objects - encapsulation.Exe) exception thrown (resolved)
- MCU is the most popular science (ten thousand words summary, worth collecting)
- 嵌入式工程师如何提高工作效率
- IDA中常见快捷键
- 5个chrome简单实用的日常开发功能详解,赶快解锁让你提升更多效率!
- 基于HPC场景的集群任务调度系统LSF/SGE/Slurm/PBS
- CAS机制
- JMeter installation
猜你喜欢
Deeply analyze the main contents of erc-4907 agreement and think about the significance of this agreement to NFT market liquidity!
555电路详解
BUUCTF---Reverse---reverse1
Encrypt and decrypt stored procedures (SQL 2008/sql 2012)
【剑指Offer】42. 栈的压入、弹出序列
字符串格式化
Programming features of ISP, IAP, ICP, JTAG and SWD
Review of the losers in the postgraduate entrance examination
Inno setup packaging and signing Guide
IIC基本知识
随机推荐
openinstall与虎扑达成合作,挖掘体育文化产业数据价值
This article explains the complex relationship between MCU, arm, muc, DSP, FPGA and embedded system
The width of table is 4PX larger than that of tbody
table宽度比tbody宽度大4px
JS实现链式调用
Common shortcut keys in IDA
IO model review
Experience sharing of software designers preparing for exams
Vs code specifies the extension installation location
串口通讯继电器-modbus通信上位机调试软件工具项目开发案例
深入分析ERC-4907协议的主要内容,思考此协议对NFT市场流动性意义!
2022.7.4DAY596
【acwing】786. Number k
BigDecimal数值比较
对word2vec的一些浅层理解
IIC Basics
Study summary of postgraduate entrance examination in November
Why is the reflection efficiency low?
Slurm资源管理与作业调度系统安装配置
字符串格式化