It means copy the array to create a same one when you want to modify it, and seperate the read and write
That is to copy the original array to write on a separate copy, so this does not affect the read on the original array; Once written, point the original array reference to the new array.
The entire ADD operation is done under the protection of the lock.This is to avoid making multiple copies when adding multiple threads concurrently, which will mess up the data and cause the final array data to be not what we expect.
If there are concurrent reads from a thread, there are several cases:
If the write operation is not completed, then directly read the data of the original array;If the write is complete, but the reference does not point to the new array, then the original array data is also read;If the write is complete and the reference is already pointing to the new array, read the data directly from the new array.CopyOnWriteArrayList is suitable for the scenario of reading more than writing, but this type is used with caution.Because there is no way to guarantee how much data CopyOnWriteArrayList will put into it, in case the data get too much and the array has to be re-copied every time add/set, this is too expensive.In high-performance Internet applications, this operation can cause a failure in minutes.