Run Scenario: Multiple threads call ArrayList at the same time to store elements
Two threads A and b, when a thread is called, there is no element in the list temporarily, at this time, the size value of the list is 0, and a when adding elements, add an element, the size of the list is still 0, in the [0] location of an element, storage, Our CPU on the A-thread task of the scheduled pause, and then start dispatching B thread, the same B-thread is also the list to do element storage operations, the list size is 0, and the B thread also adds an element to the list, at [1] position, also holds an element, resulting in this time, Our list contains two elements, and after the B thread has finished the add process, not waiting for our CPU to recover the A-thread operation, if there is a C thread to the list to do the query work, then the query to the list of size is 1, and there are two elements contained in it-- This means that the ArrayList thread is not secure,
Similarly, if it happens in HashSet, the problem also exists.
Thread safety considerations for the Operations collection--java