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

Example Analysis of CopyOnWriteArrayList Source Code in java

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail the example analysis of CopyOnWriteArrayList source code in java. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Brief introduction

CopyOnWriteArrayList is a thread-safe version of ArrayList, which is also implemented internally through arrays. Each modification to the array is completely copied to a new array, and then the old array is replaced. This ensures that only write operations are blocked, not read operations are blocked, and read-write separation is achieved.

Inheritance system public class CopyOnWriteArrayList implements List, RandomAccess, Cloneable, java.io.Serializable {...}

CopyOnWriteArrayList implements interfaces such as List,RandomAccess,Cloneable,java.io.Serializable.

CopyOnWriteArrayList implements List, providing basic operations such as add, delete, traversal, and so on.

CopyOnWriteArrayList implements RandomAccess and provides the ability to access randomly.

CopyOnWriteArrayList implements Cloneable and can be cloned.

CopyOnWriteArrayList implements Serializable and can be serialized.

The source code parsing attribute / * * is used to lock when modified * / final transient ReentrantLock lock = new ReentrantLock (); / * * where the element is really stored, you can only access * / private transient volatile Object [] array through getArray () / setArray ().

Lock: locked when modified, and decorated with transient to indicate no automatic serialization.

Array: where the element is stored, the transient modifier is used to indicate no automatic serialization, and the volatile modifier is used to indicate that one thread modifies the field and another thread can see it immediately.

Construction method

Create an empty array.

Public CopyOnWriteArrayList () {/ / all operations on array are performed by setArray (new Object [0]) through setArray () and getArray ();} final void setArray (Object [] a) {array = a;} public CopyOnWriteArrayList (Collection)

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

Development

Wechat

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

12
Report