当前位置:网站首页>ArrayList扩容机制以及线程安全性
ArrayList扩容机制以及线程安全性
2022-07-01 13:13:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
List扩容实现步骤
总的来说就是分两步:
1、扩容
把原来的数组复制到另一个内存空间更大的数组中
2、添加元素
把新元素添加到扩容以后的数组中
性能分析
ArrayList作为动态数组,其内部元素以数组形式顺序存储的,所以非常适合随机访问的场合。除了尾部插入和删除元素,往往性能会相对较差,比如我们在中间位置插入一个元素,需要移动后续所有元素。
源码分析
先把ArrayList中定义的一些属性贴出来方便下面源码分析
ArrayList的两个构造方法
1.ArrayList() 2.ArrayList(int initialCapacity)
无参构造:
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
带参构造:
public ArrayList(int initialCapacity) {
if (initialCapacity > 0) {
this.elementData = new Object[initialCapacity];
} else if (initialCapacity == 0) {
this.elementData = EMPTY_ELEMENTDATA;
} else {
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
}
}在无参构造中,创建了一个空数组,长度为0
在有参构造中,传入的参数是正整数就按照传入的参数来确定创建数组的大小,否则异常
扩容的方法
插入元素函数 (boolean add(E e))
ArrayList在执行插入元素是超过当前数组预定义的最大值时,数组需要扩容,扩容过程需要调用底层System.arraycopy()方法进行大量的数组复制操作。
贴上源码
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}看,其实add方法就两步,
第一步:增加长度
第二步:添加元素到数组
ensureCapacityInternal(int minCapacity)这个增加长度的方法
这个地方我们看到了,如果在添加的时候远数组是空的,就直接给一个10的长度,否则的话就加一
ensureExplicitCapacity(int minCapacity)
private void ensureExplicitCapacity(int minCapacity) {
modCount++;
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}通过这个地方是真正的增加长度,当需要的长度大于原来数组长度的时候就需要扩容了,相反的则不需要扩容
这个地方注意
int newCapacity = oldCapacity + (oldCapacity >> 1);
oldCapacity >> 1 右移运算符 原来长度的一半 再加上原长度也就是每次扩容是原来的1.5倍
之前的所有都是确定新数组的长度,确定之后就是把老数组copy到新数组中,这样数组的扩容就结束了
以上的一切都是ArrayList扩容的第一步,第二步就没啥说的了,就是把需要添加的元素添加到数组的最后一位
ArrayList安全性
非线程安全
1.在 add 的扩容的时候会有线程安全问题, ensureCapacityInternal(int minCapacity)这个步骤是有线程安全问题
2.在add 的elementData[size++] = e 这段代码在多线程的时候同样会有线程安全问题,
这里可以分成两个步骤:
elementData[size] = e;
size = size + 1;
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/131442.html原文链接:https://javaforall.cn
边栏推荐
- Qtdeisgner, pyuic detailed use tutorial interface and function logic separation (nanny teaching)
- 新手准备多少钱可以玩期货?农产品可以吗?
- 2.15 summary
- Class initialization and instantiation
- Wave animation color five pointed star loader loading JS special effects
- flinkcdc要实时抽取oracle,对oracle要配置什么东西?
- 不同的测试技术区分
- Investment analysis and prospect prediction report of global and Chinese dimethyl sulfoxide industry Ⓦ 2022 ~ 2028
- 路由基础之OSPF LSA详细讲解
- 9. Use of better scroll and ref
猜你喜欢

Cs5268 advantages replace ag9321mcq typec multi in one docking station scheme

Meta再放大招!VR新模型登CVPR Oral:像人一样「读」懂语音

软件测试中功能测试流程

Content Audit Technology

5G工业网关的科技治超应用 超限超重超速非现场联合执法

Professor Li Zexiang, Hong Kong University of science and technology: I'm wrong. Why is engineering consciousness more important than the best university?

【牛客刷题-SQL大厂面试真题】NO2.用户增长场景(某度信息流)

波浪动画彩色五角星loader加载js特效

mysql统计账单信息(下):数据导入及查询

Qtdeisgner, pyuic detailed use tutorial interface and function logic separation (nanny teaching)
随机推荐
Update a piece of data from the database. Will CDC get two pieces of data with OP fields D and C at the same time? I remember before, only OP was U
Yarn restart applications record recovery
Function test process in software testing
Project deployment is not difficult at all!
【开发大杀器】之Idea
arthas使用
Shangtang technology crash: a script written at the time of IPO
我花上万学带货:3天赚3元,成交靠刷单
SSO and JWT good article sorting
Reasons for MySQL reporting 1040too many connections and Solutions
8 popular recommended style layout
spark源码(五)DAGScheduler TaskScheduler如何配合提交任务,application、job、stage、taskset、task对应关系是什么?
2.15 summary
7. Icons
The popular major I chose became "Tiankeng" four years later
Content Audit Technology
JS变色的乐高积木
China NdYAG crystal market research conclusion and development strategy proposal report Ⓥ 2022 ~ 2028
ROS2 Foxy depthai_ ROS tutorial
Meta enlarge again! VR new model posted on CVPR oral: read and understand voice like a human