Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is Copy-on-Write?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report