This is a problem that Java programmers often encounter in interviews.
Work for a year or two should know that ArrayList is thread insecure, to use the thread-safe use of vectors, which is also a variety of Java interview in the book mentioned, it may be many years of work of programmers are stuck in this knowledge.
Let's start by saying why ArrayList is thread insecure, and see the code below.
/** * 公众号:Java技术栈 */public class TestArrayList { private static List<Integer> list = new ArrayList<>(); public static void main(String[] args) throws InterruptedException { for (int i = 0; i < 10; i++) { testList(); list.clear(); } } private static void testList() throws InterruptedException { Runnable runnable = () -> { for (int i = 0; i < 10000; i++) { list.add(i); } }; Thread t1 = new Thread(runnable); Thread t2 = new Thread(runnable); Thread t3 = new Thread(runnable); t1.start(); t2.start(); t3.start(); t1.join(); t2.join(); t3.join(); System.out.println(list.size()); }}
This is the result of its output, we expect the result should be: 30000, then not, this is the legendary multithreading concurrency problem.
Exception in thread "Thread-1" java.lang.ArrayIndexOutOfBoundsException: 15786 at java.base/java.util.ArrayList.add(ArrayList.java:468) at java.base/java.util.ArrayList.add(ArrayList.java:480) at com.test.thread.TestArrayList.lambda$testList$0(TestArrayList.java:23) at java.base/java.lang.Thread.run(Thread.java:844)20332161001494123749156312211827417300002869127843
Phenomenon Analysis
From the above results can be summed up in the ArrayList in the concurrency of several phenomena.
1, the occurrence of arrayindexoutofboundsexception anomalies;
private void add(E e, Object[] elementData, int s) { if (s == elementData.length) elementData = grow(); elementData[s] = e; size = s + 1;}
To locate the source code of the exception, there is no doubt that the problem is in the multi-threaded concurrent access, because there is no synchronization lock protection, resulting in ArrayList expansion inconsistent problem.
2, the normal operation of the program, output less than the actual capacity of the size;
This is also a multi-threaded concurrent assignment, the same array index location is assigned, so there is less than the expected size of the case.
3, the normal operation of the program, output the size of the expected capacity;
This is the result of normal operation, and there is no multi-threaded security problem, but it is uncertain, not every time it will achieve normal expectations.
Solution Solutions
In that case, what list collections are used to protect thread safety in high concurrency? Back to the beginning of the article, using vectors, is there anything else? Of course, space is limited, please crossing look forward to follow-up articles.
In addition, like HashMap, HashSet and so on have similar multithreading security problems, in the multi-threaded concurrency environment to avoid the use of this collection.
Reprint please indicate the original source address: original address
Information: Become the architect of the 10-stage learning materials!
Tutorial: The most powerful Spring Boot & Cloud Tutorial Summary in history
Tools: Recommend an online authoring flowchart, Mind mapping software
Scan follow our public number, reply "666" to get a set of Java concurrent Programming HD video tutorial.
A multi-line shuo with a higher exit rate than the full test