准备试验样本
实现样本
/**
* 两个线程都过了数组大小检查,先后插入数据时 引起 out of bound
* @author Geym
*
*/
public class UnsafeArrayList {
static ArrayList al=new ArrayList();
static class AddTask implements Runnable{
@Override
public void run() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {}
for(int i=0;i<1000000;i++)
al.add(new Object());
}
}
public static void main(String[] args) throws InterruptedException {
Thread t1=new Thread(new AddTask(),"t1");
Thread t2=new Thread(new AddTask(),"t2");
t1.start();
t2.start();
Thread t3=new Thread(new Runnable(){
@Override
public void run() {
while(true){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {}
}
}
},"t3");
t3.start();
}
}
ArrayList在多线程访问情况下会出现异常`java.lang.ArrayIndexOutOfBoundsException: 11`
正式起航
ArrayList内部add()内部设置断点.
debug模式启动,进入断点,显示完整堆栈,但显示的是主线程的堆栈信息
设置断点属性,使得只让t1和t2线程进入断点
挂起整个虚拟机
挂起虚拟机而不是挂起线程
调试进入ArrayList内部
ArrayList的工作方式:
Arraylist内部初始化为10个数组空间,当数组空间消耗完毕后,arraylist就会自动扩容.在每次add()函数时,系统总要事先检查下内部空间是否满足所需的大小.
多线程情况下,当arraylist容量快用完时,两个线程同时进入add()函数,并同时认为不需要扩容,且各自写入自己的数据,那么很可能有一个线程会将数据写到边界外,从而产生ArrayIndexOutOfBoundsException.
设置断点,模拟到达线程扩容临界点9:
t1和t2线程均进入方法:
t1线程执行到下一步,切换t2线程:
t2线程执行到下一步,切换回t1线程进行添加元素:
t2线程抛出异常,ArrayIndexOutOfBoundsException:
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。