ArraysList Is a thread unsafe data type . If multiple threads access the list instance at the same time , And at least one thread modifies the list structurally , External synchronization is required . Use
Collections.sychronizedList Method is thread safe .

See JDK File , Modify on structure ArraysList There are ways

Analyze source code discovery , stay add Method in elementData[size++] = e; Risk of thread insecurity .

elementData And size Are global variables , But it didn't sychronization Synchronous processing ,elementData Is a shared thread unsafe mutable Variable data .
public class ArrayList<E> extends AbstractList<E> implements List<E>,
RandomAccess, Cloneable, java.io.Serializable { private static final long
serialVersionUID= 8683452581122892189L; private static final int
DEFAULT_CAPACITY= 10; private static final Object[] EMPTY_ELEMENTDATA = {};
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {}; transient
Object[] elementData; // non-private to simplify nested class access private int
size; .................. public boolean add(E e) { ensureCapacityInternal(size
+ 1); // Increments modCount!! elementData[size++] = e; return true; } public
void add(int index, E element) { rangeCheckForAdd(index); ensureCapacityInternal
(size + 1); // Increments modCount!! System.arraycopy(elementData, index,
elementData, index + 1, size - index); elementData[index] = element; size++; } }
stay iterator Some risk of thread insecurity , One of the reasons is iterator Some of them are directly used ArraysList Of add Method
ArrayList.this.add(i, e); , But it didn't sychronization Synchronous processing .
public ListIterator<E> listIterator(int index) { if (index < 0 || index > size)
throw new IndexOutOfBoundsException("Index: "+index); return new ListItr(index);
} public ListIterator<E> listIterator() { return new ListItr(0); } public
Iterator<E> iterator() { return new Itr(); } /** * An optimized version of
AbstractList.Itr */ private class Itr implements Iterator<E> { .................
...... } /** * An optimized version of AbstractList.ListItr */ private class
ListItr extends Itr implements ListIterator<E> { ........................ public
void set(E e) { if (lastRet < 0) throw new IllegalStateException();
checkForComodification(); try { ArrayList.this.set(lastRet, e); } catch (
IndexOutOfBoundsException ex) { throw new ConcurrentModificationException(); } }
public void add(E e) { checkForComodification(); try { int i = cursor; ArrayList
.this.add(i, e); cursor = i + 1; lastRet = -1; expectedModCount = modCount; }
catch (IndexOutOfBoundsException ex) { throw new ConcurrentModificationException
(); } } }
<> summary

This passage add and Iterator Start analysis ArraysList Reasons for thread insecurity , May not be comprehensive enough , Welcome to add , Welcome to correct. !

Technology
©2019-2020 Toolsou All rights reserved,
One is called “ Asking for the train ” A small village Finally got the train Spring Boot Lesson 16 :SpringBoot Implementation of multithreading with injection class Chrome OS, For programmers and Windows What does it mean ? Internet Marketing JAVA Convert a string to a numeric type I've been drinking soft water for three years ? What is the use of soft water and water softener You don't know ——HarmonyOS Talking about uni-app Page value transfer problem JavaScript Medium Call and ApplySparkSQL Achieve partition overlay write Character recognition technology of vehicle license plate based on Neural Network