1. Lock (TODO)
2. Copy the container when writing
The Copyonwrite container is the container that is copied when it is written. The popular understanding is that when we add elements to a container, instead of adding them directly to the current container, the current container is copied, a new container is duplicated, the new container is added, the element is added, and the reference to the original container is pointed to the new container. Copyonwrite concurrent containers are used for read-write-less concurrency scenarios. The Java concurrency Package provides two concurrent containers implemented using the Copyonwrite mechanism, which are copyonwritearraylist and Copyonwritearrayset (write-time replication containers without a map, which can be implemented on their own).
The advantage of this is that we can read the Copyonwrite container concurrently, without having to lock it, because the current container does not add any elements. So the Copyonwrite container is also a reading and writing separation of ideas, reading and writing different containers.
The Copyonwrite container has many advantages, but there are also two problems, namely, memory occupancy and data consistency . The Copyonwrite container can only guarantee the final consistency of the data and cannot guarantee the real-time data consistency. So if you want to write the data that can be read right away, please do not use the Copyonwrite container.
http://ifeve.com/java-copy-on-write/
3.
Java Concurrent Notes