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, { 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. !

©2019-2020 Toolsou All rights reserved,
java Comparing attribute values between two objects utilize Python handle Excel data ——xlrd,xlwt library Bidirectional linked list Why? Python Not a future oriented programming language ?Python【 Assignment statement 】 Special lecture , You can't just a=b ah ! Suggest mastering ! utilize Python handle Excel data ——pandas library see SQL-SERVER Data volume and occupied space of database and each table PID The algorithm finally figured out the principle , It was so simple web Two front-end practical games ( Attached source code ) Beginners learn Python Be sure to know what his basic algorithms are ? What is the function ?