当前位置:网站首页>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
边栏推荐
- [development of large e-commerce projects] performance pressure test - basic concept of pressure test & jmeter-38
- Google Earth engine (GEE) - Global Human Settlements grid data 1975-1990-2000-2014 (p2016)
- Introduction to topological sorting
- MySQL六十六问,两万字+五十图详解!复习必备
- 1.8新特性-List
- Analysis report on the development trend and Prospect of new ceramic materials in the world and China Ⓐ 2022 ~ 2027
- CV顶会最佳论文得主分享:好论文是怎么炼成的?
- Meta再放大招!VR新模型登CVPR Oral:像人一样「读」懂语音
- Apache-Atlas-2.2.0 独立编译部署
- leetcode 322. Coin change (medium)
猜你喜欢

The popular major I chose became "Tiankeng" four years later

华为HMS Core携手超图为三维GIS注入新动能

Hardware development notes (9): basic process of hardware development, making a USB to RS232 module (8): create asm1117-3.3v package library and associate principle graphic devices

Detailed explanation of OSPF LSA of routing Foundation

我花上万学带货:3天赚3元,成交靠刷单

codeforces -- 4B. Before an Exam

Fiori 应用通过 Adaptation Project 的增强方式分享

Application of 5g industrial gateway in scientific and technological overload control; off-site joint law enforcement for over limit, overweight and overspeed

What is the future development direction of people with ordinary education, appearance and family background? The career planning after 00 has been made clear

MySQL statistical bill information (Part 2): data import and query
随机推荐
arthas使用
Operator-1初识Operator
北斗通信模块 北斗gps模块 北斗通信终端DTU
The stack size specified is too small, specify at least 328k
MySQL statistical bill information (Part 2): data import and query
C language learning
JS变色的乐高积木
王兴的无限游戏迎来“终极”一战
彩色五角星SVG动态网页背景js特效
PG基础篇--逻辑结构管理(触发器)
String input function
Camp division of common PLC programming software
c语言学习
数字化转型再下一城,数字孪生厂商优锘科技宣布完成超3亿元融资
Zabbix 6.0 源码安装以及 HA 配置
简单的两个圆球loading加载
华为HMS Core携手超图为三维GIS注入新动能
Router.use() requires a middleware function but got a Object
啟動solr報錯The stack size specified is too small,Specify at least 328k
SVG钻石样式代码