Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is Copy-on-Write?

Shulou Source: shulou.com Published: 2022-06-01 07:19:23 10月01日 Update

This article introduces you what is Copy-on-Write, the content is very detailed, interested friends can use for reference, hope to be helpful to you.

Introduction: Copy-on-Write 's so-called Copy-on-Write, is very simple, as the name implies, that is, "write data using a copy of the copy to execute". The two Copy-on-Write containers, CopyOnWriteArrayList and CopyOnWriteArraySet, are commonly used in Java. The design idea behind them is that the read operation implemented by Copy-on-Write; through the Copy-on-Write container is lock-free. Since there is no lock, the performance of the read operation is brought to the extreme source code analysis:

Let's take a look at the CopyOnWriteArrayList source code, and first take a look at the add method:

Public boolean add (E e) {final ReentrantLock lock = this.lock; lock.lock (); try {Object [] elements = getArray (); int len = elements.length; Object [] newElements = Arrays.copyOf (elements, len + 1); newElements [len] = e; setArray (newElements); return true } finally {lock.unlock ();}} you can see that when you add, you first acquire the lock through cas, and then get the original array length.

Call copyOf to copy the original array to the new array with the length of the original array + 1

Then call the setArray method, that is, when you modify it, the actual call is a copy of the source array, so let's further follow up the setArray method / * Sets the array. * / final void setArray (Object [] a) {array = a;}

You can see that the new array is copied directly to array; here. Then who is array? knock on the blackboard! Here comes the key point.

Private transient volatile Object [] array

Internal static variables, with volatile modification, then you can assign this copy array to the reference variable of the array modified by volatile as written by volatile.

As soon as a value is assigned to the volatile-decorated variable, it will immediately be visible to the reader thread, and everyone will be able to see the latest array.

Is it difficult to read love and beg to come at the same time as it is added? Will it block? Let's move on to the get method:

Public E get (int index) {return get (getArray (), index);}

Final Object [] getArray () {return array;}

It will not be blocked or unsafe at all, because it is modified with a copy of the source array, which is very simple to get from the internal static variable array according to the subscript.

CopyOnWriteArrayList is to trade space for time, update based on copy to avoid locks, and then use volatile variable to assign values to ensure visibility, which is suitable for scenarios with more reads and less writes. Therefore, it is necessary to judge which is suitable according to the actual business scenario. On what is Copy-on-Write to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Array copy variable time that is method length two content scene actual container more source code static help update good medium as the name implies Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi macOS NVidia Shulou Technology Shulou Information