Copy-on-write abbreviation Cow, is a kind of optimization strategy used in program design.
There are two types of cow containers in the JDK: copyonwritearraylist and Copyonwritearrayset,cow containers are very useful and can be used in very many concurrent scenarios.
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, we do not add them directly to the current container, but instead copy the current container, copying a new container.
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 the mind, read and write containers.
This also shows that the application scenario of cow container is best to read and write less scenes, otherwise it will produce a lot of memory duplication and recycling problems.
Public Static void Main (string[] args) { // containers are used in the same way as the list and/ or write operations of multiple threads, There will be a re-entry lock to ensure that the write operation does not occur with data inconsistency. New copyonwritearraylist<string>(); CopyonwritearraysetNew copyonwritearrayset<string>(); }
Copy-on-write container