一、添加单个元素数组越界分析
add源码如下
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
size字段的定义
The size of the ArrayList (the number of elements it contains).
ArrayList的大小(包含元素的个数)
elementData[size++]=e
此代码分2个步骤,先执行赋值,既elementData[size]=e,然后在执行size++
模拟越界场景
定义一个数组长度为10
ArrayList list = new ArrayList(10);
假设已经添加九个元素,所以size = 9
thread 1执行add,会判断ensureCapacityInternal(size + 1),size + 1<10,不进行扩容
thread 2执行add,会判断ensureCapacityInternal(size + 1),size + 1<10,不进行扩容
以上thread 1和thread 2同时运行
thread 1执行elementData[size++] = e;此时size++后等于10
thread 2执行elementData[size++] = e;此时size==10, elementData[10]抛出out of index错误
因此验证在并行情况下,扩容临界值执行add会数组越界
二、添加集合越界分析
addList源码
public boolean addAll(Collection<? extends E> c) {
Object[] a = c.toArray();
int numNew = a.length;
ensureCapacityInternal(size + numNew); // Increments modCount
System.arraycopy(a, 0, elementData, size, numNew);
size += numNew;
return numNew != 0;
}
数组越界原理同上,两个线程都刚好执行ensureCapacityInternal(size + numNew);且都不需要进行扩容,
在执行System.arraycopy(a, 0, elementData, size, numNew)发生越界
三、解决方案并分析
使用Collections.synchronizedList(new ArrayList<>())替代ArrayList
如果是读多写少的场景,使用CopyOnWriteArrayList替代ArrayList
collections源码分析
public static <T> List<T> synchronizedList(List<T> list) {
return (list instanceof RandomAccess ?
new SynchronizedRandomAccessList<>(list) :
new SynchronizedList<>(list));
}
最终new 出SynchronizedRandomAccessList类
static class SynchronizedRandomAccessList<E>
extends SynchronizedList<E>
implements RandomAccess
SynchronizedRandomAccessList继承SynchronizedList
static class SynchronizedList<E>
extends SynchronizedCollection<E>
implements List<E> {
接着又继承SynchronizedCollection
static class SynchronizedCollection<E> implements Collection<E>, Serializable {
private static final long serialVersionUID = 3053995032091335093L;
final Collection<E> c; // Backing Collection
final Object mutex; // Object on which to synchronize
SynchronizedCollection(Collection<E> c) {
this.c = Objects.requireNonNull(c);
mutex = this;
}
SynchronizedCollection(Collection<E> c, Object mutex) {
this.c = Objects.requireNonNull(c);
this.mutex = Objects.requireNonNull(mutex);
}
public int size() {
synchronized (mutex) {return c.size();}
}
public boolean isEmpty() {
synchronized (mutex) {return c.isEmpty();}
}
public boolean contains(Object o) {
synchronized (mutex) {return c.contains(o);}
}
public Object[] toArray() {
synchronized (mutex) {return c.toArray();}
}
public <T> T[] toArray(T[] a) {
synchronized (mutex) {return c.toArray(a);}
}
public Iterator<E> iterator() {
return c.iterator(); // Must be manually synched by user!
}
public boolean add(E e) {
synchronized (mutex) {return c.add(e);}
}
public boolean remove(Object o) {
synchronized (mutex) {return c.remove(o);}
}
public boolean containsAll(Collection<?> coll) {
synchronized (mutex) {return c.containsAll(coll);}
}
public boolean addAll(Collection<? extends E> coll) {
synchronized (mutex) {return c.addAll(coll);}
}
public boolean removeAll(Collection<?> coll) {
synchronized (mutex) {return c.removeAll(coll);}
}
public boolean retainAll(Collection<?> coll) {
synchronized (mutex) {return c.retainAll(coll);}
}
public void clear() {
synchronized (mutex) {c.clear();}
}
public String toString() {
synchronized (mutex) {return c.toString();}
}
// Override default methods in Collection
@Override
public void forEach(Consumer<? super E> consumer) {
synchronized (mutex) {c.forEach(consumer);}
}
@Override
public boolean removeIf(Predicate<? super E> filter) {
synchronized (mutex) {return c.removeIf(filter);}
}
@Override
public Spliterator<E> spliterator() {
return c.spliterator(); // Must be manually synched by user!
}
@Override
public Stream<E> stream() {
return c.stream(); // Must be manually synched by user!
}
@Override
public Stream<E> parallelStream() {
return c.parallelStream(); // Must be manually synched by user!
}
private void writeObject(ObjectOutputStream s) throws IOException {
synchronized (mutex) {s.defaultWriteObject();}
}
}
看得出,使用了final Collection<E> c存放具体集合类,
每个方法使用synchronized进行同步限制,锁对象为mutex=this,
四、总结
1.ArrayList不适合多线程场景使用
2.线程安全List有Collections.synchronizedList和CopyOnWriteArrayList
3.Collections集合类很多好用的方法,注意Collection是接口,写法上区分有没有s
4.静态内部类synchronizedList 继承-> SynchronizedRandomAccessList 继承-> SynchronizedCollection
5.除了静态内部类synchronizedList,同理有静态内部类synchronizedSet和静态内部类synchronizedMap