Deep Java Collection Series VI: COPYONWRITEARRAYLIST__ collection Framework

Source: Internet
Author: User
Tags dateformat

copyonwritearraylist Introduction

The Copyonwritearraylist container is an alternative to collections.synchronizedlist (List list), copyonwritearraylist in some cases has better performance, Consider reading a much larger-than-written scenario, and if you lock all of the read operations, because only one read thread can get the lock, other read threads must wait and greatly affect performance. Copyonwritearraylist is called a "write-time Copy" container, where you copy a container when you are multithreaded to manipulate the container object, so that changes within the thread are not related to other threads, and this design can be done without blocking other read threads. The Java concurrency package, starting with JDK1.5, provides two concurrent containers implemented using the Copyonwrite mechanism, which are copyonwritearraylist and Copyonwritearrayset.

copyonwritearraylist Container Usage Example

The following code shows how to use the Copyonwritearraylist container:

Package com.rhwayfun.patchwork.concurrency.r0408;
Import Java.text.DateFormat;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
Import java.util.List;
Import java.util.concurrent.CopyOnWriteArrayList;
Import Java.util.concurrent.ExecutorService;
Import java.util.concurrent.Executors;

Import Java.util.concurrent.atomic.AtomicLong;
 /** * Created by Rhwayfun on 16-4-8.

    * * public class Copyonwritearraylistddemo {/** * content number * * * private static Atomiclong contentnum;

    /** * Date Format device */private static DATEFORMAT format;

    /** * thread Pool * * Private final executorservice ThreadPool;
        Public Copyonwritearraylistddemo () {contentnum = new Atomiclong ();
        format = new SimpleDateFormat ("HH:mm:ss");
    ThreadPool = Executors.newfixedthreadpool (10); public void doexec (int num) throws interruptedexception {list<string> List = new Copyonwritearraylis
        T<> (); for (int i = 0; i < num; i++) {List.add (i, "main-content-" + i);
        ///5 write thread for (int i = 0; i < 5; i++) {Threadpool.execute (new Writer (list,i));
        //Start 10 read threads for (int i = 0; i < i++) {Threadpool.execute (new Reader (list));
    ///Close thread pool Threadpool.shutdown (); /** * Write Thread * * @author Rhwayfun */Static class Writer implements Runnable {privat
        e final list<string> copyonwritearraylist;

        private int i; Public Writer (list<string> copyonwritearraylist,int i) {this.copyonwritearraylist = Copyonwritearraylis
        THIS.I = i; @Override public void Run () {Copyonwritearraylist.add (i, "content-" + Contentnum.incrementa
            Ndget ()); System.out.println (Thread.CurrentThread (). GetName () + ": Write content-" + contentnum.get () + "" +for Mat.format (New Date ());
        System.out.println (Thread.CurrentThread (). GetName () + ": Remove" + copyonwritearraylist.remove (i));

        } Static class Reader implements Runnable {private final list<string> List;
        Public Reader (list<string> list) {this.list = list; @Override public void Run () {A for (String s:list) {System.out.println (Th
            Read.currentthread (). GetName () + ": read" + S + "" +format.format (New Date ()); }} public static void Main (string[] args) throws Interruptedexception {COPYONWRITEARRAYLISTDD
        Emo demo = new Copyonwritearraylistddemo ();
    Demo.doexec (5);

First start 5 write threads, then start 10 read threads, run the program to find that there is no exception, so the use of write-time replication container is highly efficient. The code runs as follows:

copyonwritearraylist Source Analysis

First of all, the principle of the implementation of the Copyonwritearraylist container: simply, when the container is required to operate, the container copies a copy of the container, such as the modification of the container in the copy of the operation, when the operation is completed, and then the container container copy point to the original container. The advantage of this design is that the read-write separation is achieved, and reading does not block. The following source code is the Copyonwritearraylist Add method implementation:

public void Add (int index, E Element) {final Reentrantlock lock = This.lock;
        Lock.lock ();
            try {object[] elements = GetArray ();
            int len = elements.length;
                                                    if (Index > Len | | Index < 0) throw new Indexoutofboundsexception ("Index:" +index+
            ", Size:" +len);
            Object[] newelements;
            int nummoved = Len-index;
            1, copy out a new array if (nummoved = = 0) newelements = arrays.copyof (elements, Len + 1);
                else {newelements = new Object[len + 1];
                System.arraycopy (elements, 0, newelements, 0, index);
            System.arraycopy (elements, index, newelements, index + 1, nummoved);
            }//2, add new elements to the new array newelements[index] = element;
        3, point the array to the original array setarray (newelements); } Finally {lock.unlock (); }

The three steps above implement the idea of copying in writing, which does not lock the list when reading data, because the write operation is done on the original container copy. And, as you can see, if there is a new read thread in the process of modifying the copy of the container, the old data is still being read. Read the following code:

Public E get (int index) {return get
        (GetArray (), index);
    Final object[] GetArray () {return

Copyonwrite concurrent containers are used to read and write less concurrent scenes. such as whitelist, blacklist, the purpose of the commodity to access and update the scene.

The disadvantage of copyonwritearraylist

The principle of copyonwritearraylist can be seen because of the need to copy the container object, so there are two problems: memory footprint Problem and data consistency problem .

Memory footprint Problem: Because the original object needs to be copied, this requires a certain amount of overhead. Especially when the container object is too large, the memory occupied by the copy will increase by one times (the original memory object is still in use, after the copy there are two objects in memory, so add a memory). Also, in highly concurrent scenarios, this is more evident because each thread copies an object in memory. Because of the JVM's optimization mechanism, the frequent young GC and full GC will be triggered, which can degrade the performance of the entire system.

Data consistency problem: Copyonwritearraylist does not guarantee real-time consistency because the read thread reads the data again before it is pointed back to the original object, so copyonwritearraylist can only guarantee final consistency. Therefore, in the need for real-time consistency of the factory several copyonwritearraylist is not available.

Copyonwritearraylist Summary: Copyonwritearraylist applies to less read and write scenarios that do not throw concurrentmodificationexception when a container object is concurrently manipulated, And the returned elements are consistent with the elements created by the iterator. The replication of the container object requires a certain amount of overhead, and if the object occupies too much memory, it may cause frequent younggc and full GC copyonwritearraylist cannot guarantee real-time data consistency, only guarantee final consistency Use copyonwritearraylist

When concurrent operations list objects are required.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.