集合类线程安全问题
ArrayList
先不谈ArrayList线程安不安全问题,看其源码可以知道,我们在new一个ArrayList的时候,其实底层使用的是数组作为数据结构的。
/**
* Default initial capacity.
*/
private static final int DEFAULT_CAPACITY = 10;
/**
* Shared empty array instance used for empty instances.
*/
private static final Object[] EMPTY_ELEMENTDATA = {};
/**
* Shared empty array instance used for default sized empty instances. We
* distinguish this from EMPTY_ELEMENTDATA to know how much to inflate when
* first element is added.
*/
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
/**
* The array buffer into which the elements of the ArrayList are stored.
* The capacity of the ArrayList is the length of this array buffer. Any
* empty ArrayList with elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA
* will be expanded to DEFAULT_CAPACITY when the first element is added.
*/
transient Object[] elementData; // non-private to simplify nested class access
/**
* The size of the ArrayList (the number of elements it contains).
*
* @serial
*/
private int size;
/**
* Constructs an empty list with the specified initial capacity.
*
* @param initialCapacity the initial capacity of the list
* @throws IllegalArgumentException if the specified initial capacity
* is negative
*/
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);
}
}
/**
* Shared empty array instance used for default sized empty instances. We
* distinguish this from EMPTY_ELEMENTDATA to know how much to inflate when
* first element is added.
*/
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
可以看到,ArrayList有两个制表,一个是size,当前数组长度,一个是capacity存储最大容量,size的初始值是0,capacity初始值是10.
/**
* Appends the specified element to the end of this list.
*
* @param e element to be appended to this list
* @return <tt>true</tt> (as specified by {@link Collection#add})
*/
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
当执行add操作的时候,会进行capacity的确认计算。在add方法中,如果数组长度为0,则分配10capacity,此外如果增加后的minCapacity(minCapacity = size + 1),minCapacity > 底层数据结构数组的长度,则进行扩容,如下所示。
private void ensureExplicitCapacity(int minCapacity) {
modCount++;
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
/**
* The maximum size of array to allocate.
* Some VMs reserve some header words in an array.
* Attempts to allocate larger arrays may result in
* OutOfMemoryError: Requested array size exceeds VM limit
*/
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
/**
* Increases the capacity to ensure that it can hold at least the
* number of elements specified by the minimum capacity argument.
*
* @param minCapacity the desired minimum capacity
*/
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);
}
private static int hugeCapacity(int minCapacity) {
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}
扩容的关键在这一行:int newCapacity = oldCapacity + (oldCapacity >> 1);右移一位为除以2的1次方,也就是扩容1.5倍。值得注意的是,在计算机中,乘除都耗内存,用移位运算速度会快很多。
并发修改异常
public static void main(String[] args) {
List<String> list = new ArrayList<>();
for (int i = 0; i < 30; i++) {
new Thread(() -> {
list.add(UUID.randomUUID().toString().substring(0, 8));
System.out.println(list);
}).start();
}
}
java.util.ConcurrentModificationException,发生并发修改异常。之所以发生并发修改异常就是不同的线程抢占临界资源,造成修改紊乱。
ArrayList实现并发修改异常的方式也是基于CAS原理,其中通过ModCount和期望值expectedModCount作对比,如果不是期望值则抛出该异常。
为什么出现
多线程并发调用修改性质的操作方法,势必会改乱,这里不做过多解释。
解决方案
既然是并发修改紊乱引起的异常,只要处理好并发安全即可,所以让资源类中,有修改性质的代码块只有一个线程能够执行。
-
加锁,synchronized,在修改操作的代码块。这样的话基本上算是同步执行了。
public static void main(String args[]) { List<String> list = new ArrayList<>(); for (int i = 0; i < 30; i++) { new Thread(() -> { synchronized (ArrayListTest.class){ list.add(UUID.randomUUID().toString().substring(0, 8)); System.out.println(list); } }).start(); } }
-
使用vector,可以看到它的源码,其实还是在修改操作的方法上加锁
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-IjgmaAa4-1578637462920)(http://pzgfyl5h0.bkt.clouddn.com/vector%E5%8A%A0%E9%94%81.png)]
-
使用集合工具类Collections得到synchronizedList,可以看到源码还是加synchronized
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-L69Zxhhw-1578637462924)(http://pzgfyl5h0.bkt.clouddn.com/synchronizedList.png)]
public static void main(String args[]) { List<String> list = Collections.synchronizedList(new ArrayList<>()); for (int i = 0; i < 30; i++) { new Thread(() -> { list.add(UUID.randomUUID().toString().substring(0, 8)); System.out.println(list); }).start(); } }
-
使用CopyOnWriteArrayList,写时复制思想。
public static void main(String args[]) { List<String> list = new CopyOnWriteArrayList<>(); for (int i = 0; i < 30; i++) { new Thread(() -> { list.add(UUID.randomUUID().toString().substring(0, 8)); System.out.println(list); }).start(); } }
写时复制
每次修改操作都会创建一个副本对象,读取时读取的还是原来的原本对象,这样读取效率就会大大提高,但是读取的数据即时性得不到保证,且副本对象使用Arrays.copyOf,内存开销较大。在修改完后将引用有原本指向副本,下一次读就可以读到新更新的内容了。从其add源码中就可以看到:
/**
* Appends the specified element to the end of this list.
*
* @param e element to be appended to this list
* @return {@code true} (as specified by {@link Collection#add})
*/
public boolean add(E e) {
synchronized (lock) {
Object[] elements = getArray();
int len = elements.length;
Object[] newElements = Arrays.copyOf(elements, len + 1);
newElements[len] = e;
setArray(newElements);
return true;
}
}
- 适用场景:读操作频率远远大于写操作
- 优点:读取并发性能大大提升
- 缺点:
- 读取的数据可能不是即时数据
- 副本对象占用内存开销
其他unsafe集合类
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9cM0Bhu7-1578637462927)(http://pzgfyl5h0.bkt.clouddn.com/%E9%9B%86%E5%90%88%E7%B1%BB%E7%BA%BF%E7%A8%8B%E4%B8%8D%E5%AE%89%E5%85%A8.png)]
同理,只要在Collections集合工具类中可以使普通集合类synchronize的,都是线程不安全的。所以ArrayList, Map, Set都是线程不安全的集合类,解决办法也都类似,这里不再赘述。
来源:CSDN
作者:徒破壁
链接:https://blog.csdn.net/qq_32127759/article/details/103923849