当前位置:网站首页>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
边栏推荐
- 机器学习—性能度量
- 7. Icons
- The future of game guild in decentralized games
- Apache-atlas-2.2.0 independent compilation and deployment
- 【机器学习】VAE变分自编码器学习笔记
- When Sqlalchemy deletes records with foreign key constraints, the foreign key constraints do not work. What is the solution?
- Look at the sky at dawn and the clouds at dusk, and enjoy the beautiful pictures
- C language learning
- Mysql间隙锁
- CS5268优势替代AG9321MCQ Typec多合一扩展坞方案
猜你喜欢

VM虚拟机配置动态ip和静态ip访问

香港科技大学李泽湘教授:我错了,为什么工程意识比上最好的大学都重要?

Jenkins+webhooks- multi branch parametric construction-

JS变色的乐高积木

内容审计技术

Introduction to reverse debugging PE structure input table output table 05/07

我选的热门专业,四年后成了“天坑”

Computer network interview knowledge points

Feign & Eureka & zuul & hystrix process

Fiori 应用通过 Adaptation Project 的增强方式分享
随机推荐
请问flink mysql cdc 全量读取mysql某个表数据,对原始的mysql数据库有影响吗
flinkcdc要实时抽取oracle,对oracle要配置什么东西?
8 popular recommended style layout
Feign & Eureka & zuul & hystrix process
Vs code set code auto save
When Sqlalchemy deletes records with foreign key constraints, the foreign key constraints do not work. What is the solution?
Qtdeisgner, pyuic detailed use tutorial interface and function logic separation (nanny teaching)
Analysis report on the development prospect and investment strategy of the global and Chinese laser chip industry Ⓑ 2022 ~ 2027
Look at the sky at dawn and the clouds at dusk, and enjoy the beautiful pictures
我花上万学带货:3天赚3元,成交靠刷单
Who should I know when opening a stock account? Is it actually safe to open an account online?
Apache-atlas-2.2.0 independent compilation and deployment
Beidou communication module Beidou GPS module Beidou communication terminal DTU
JS discolored Lego building blocks
香港科技大学李泽湘教授:我错了,为什么工程意识比上最好的大学都重要?
La taille de la pile spécifiée est petite, spécifiée à la sortie 328k
Class initialization and instantiation
MySQL gap lock
Apache-Atlas-2.2.0 独立编译部署
JS变色的乐高积木