CopyOnWriteArrayList and CopyOnWriteArraySet

For the ArrayList and HashSet collections, both are thread-unsafe. There will be problems under concurrent operation, and ConcurrentModificationException (concurrent modification exception) will be reported, although they also have corresponding thread-safe classes. For example, ArrayList corresponds to Vector, which is thread-safe. But looking at the underlying source code, you know that many methods at the bottom of the Vector class add the synchronized keyword to lock to achieve thread safety. This will lead to very low efficiency, so it is generally not used under concurrent operations.

Collections a collection of tools which provide us with some of the ways you can convert thread-safe class is thread-safe class, for example Collections.synchronizedList can be converted to a thread-safe List List thread-safe, such as List<Integer> list = Collections.synchronizedList(new ArrayList<>());the underlying implementation is to create The SynchronizedList class, and the SynchronizedList class is a static internal class in java.util.Collections. The reason why thread safety can be guaranteed is that synchronization code blocks are used in some common method processing. Compared with Vector, the advantage of thread safety is that the scope of the lock is smaller and the efficiency is higher. In addition, the object to be locked can be specified flexibly , And the synchronization method can only lock this or the Class object of the class; in the same way, Collections.synchronizedSet converts a thread-unsafe Set to a thread-safe Set and Collections.synchronizedMap converts a thread-unsafe Map to a thread-safe Map. all the same

In juc ​​concurrency, CopyOnWriteArrayList is generally used as the thread-safe class corresponding to ArrayList, and CopyOnWriteArraySet is used as the thread-safe class corresponding to HashSet

The idea of ​​CopyOnWrite (COW) is an optimization strategy in the field of computer programming. It means that when a block of memory is modified, the original memory block is not written directly, but a copy of the memory is performed, and the write operation is performed in the new memory. After the writing is completed, the original memory pointer is pointed to With the new memory, the original memory can be reclaimed.

CopyOnWriteArrayList and CopyOnWriteArraySet use the COW idea. When performing a read operation, no lock is added. Because the read operation does not involve data modification, there is no thread insecurity problem, so no lock can improve efficiency. When performing a write operation, it is not directly modified on the original data, but a copy of the original data, the modification is made on this copy, and after writing, the modified copy is replaced with the original data. This ensures that the write operation will not affect the read operation. That is to say, the read operation will not be blocked when writing halfway, because the original data is still read. In addition, Lock is added to the add method to ensure synchronization, avoiding multiple copies when multi-threaded writing.

In summary, CopyOnWriteArrayList and CopyOnWriteArraySet are suitable for scenarios with more reads and less writes, because the read operation is not locked and efficient, and the write operation has to copy one copy each time, which will take up more memory; in addition, CopyOnWriteArrayList and CopyOnWriteArraySet are with traditional thread safety The advantage over the Vector class is that all operations of the Vector class (addition, deletion, modification, and check) are locked, while CopyOnWriteArrayList and CopyOnWriteArraySet only add locks for addition, deletion, modification, and read operations without locking, which is more efficient

Reference article: https://www.jianshu.com/p/9b6a4d0b94ac

Guess you like

Origin blog.csdn.net/can_chen/article/details/108686063