Friday, May 17, 2024
HomeJavaWhat's CopyOnWriteArrayList in Java - Instance Tutorial

What’s CopyOnWriteArrayList in Java – Instance Tutorial


CopyOnWriteArrayList vs Array Record in Java

CopyOnWriteArrayList is a concurrent Assortment class launched in Java 5 Concurrency API together with its fashionable cousin ConcurrentHashMap in Java. CopyOnWriteArrayList implements Record interface like ArrayList, Vector, and LinkedList however its a thread-safe assortment and it achieves its thread-safety in a barely totally different method than Vector or different thread-safe assortment class. Because the title suggests CopyOnWriteArrayList creates a replica of underlying ArrayList with each mutation operation e.g. add, take away, or while you set values. That is why it is just appropriate for a small listing of values that are learn often however modified hardly ever e.g. a listing of configurations.

Usually CopyOnWriteArrayList could be very costly as a result of it entails expensive Array copy with each writes operation but it surely’s very environment friendly you probably have a
Record the place Iteration outnumbers mutation e.g. you largely have to iterate the ArrayList and do not modify it too usually.

Iterator of CopyOnWriteArrayList is fail-safe and would not throw ConcurrentModificationException even when underlying CopyOnWriteArrayList is modified as soon as Iteration begins as a result of Iterator is working on a separate copy of ArrayList. Consequently, all of the updates made on CopyOnWriteArrayList just isn’t out there to Iterator (see Java Fundamentals: Collections).

On this Java Assortment tutorial we are going to see What’s CopyOnWriteArrayList in Java, Distinction between ArrayList and CopyOnWriteArrayList in Java and One easy Java program instance on use CopyOnWriteArrayList in Java.

Distinction between CopyOnWriteArrayList and ArrayList in Java.

Within the final part, we’ve seen What’s CopyOnWriteArrayList in Java and The way it achieves thread-safety by making a separate copy of Record for every writes operation.

Now let’s examine Some distinction between ArrayList and CopyOnWriteArrayList in Java, which is one other implementation of Record interface :

1) Before everything distinction between CopyOnWriteArrayList and ArrayList in Java is that CopyOnWriteArrayList is a thread-safe assortment whereas ArrayList just isn’t thread-safe and can’t be used within the multi-threaded atmosphere.
2) The second distinction between ArrayList and CopyOnWriteArrayList is that Iterator of ArrayList is fail-fast and throw ConcurrentModificationException as soon as detect any modification in Record as soon as iteration begins however Iterator of CopyOnWriteArrayList is fail-safe and would not throw ConcurrentModificationException.
3) The third distinction between CopyOnWriteArrayList vs ArrayList is that Iterator of former would not help take away operation whereas Iterator of later helps take away() operation.  If you wish to study extra about collections, I counsel you undergo Full Java MasterClass, probably the greatest Java course on Udemy.

CopyOnWriteArrayList Instance in Java

CopyOnWriteArrayList Example in Java - Difference with ArrayListHere’s a full code Instance of CopyOnWriteArrayList which exhibit that Iterator of CopyOnWriteArrayList would not help take away() operation.
import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 *
 * Java program to exhibit What’s CopyOnWriteArrayList in Java,

 * Iterator of CopyOnWriteArrayList
 * doesn’t help add, take away or any modification operation.
 *
 * @creator Java67
 */

public class CopyOnWriteArrayListExample{

    public static void fundamental(String args[]) {
     
        CopyOnWriteArrayList<String> threadSafeList = new CopyOnWriteArrayList<String>();
        threadSafeList.add(“Java”);
        threadSafeList.add(“J2EE”);
        threadSafeList.add(“Assortment”);
     
        //add, take away operator just isn’t supported by CopyOnWriteArrayList iterator
        Iterator<String> failSafeIterator = threadSafeList.iterator();
        whereas(failSafeIterator.hasNext()){
            System.out.printf(“Learn from CopyOnWriteArrayList : %s %n”, failSafeIterator.subsequent());
            failSafeIterator.take away(); //not supported in CopyOnWriteArrayList in Java
        }
    }
}

Output:
Learn from CopyOnWriteArrayList : Java
Learn from CopyOnWriteArrayList : J2EE
Learn from CopyOnWriteArrayList : Assortment

If we uncomment, commented code on this Java program which modifies CopyOnWriteArrayList utilizing Iterator then we are going to get following Exception :

Learn from CopyOnWriteArrayList : Java

Exception in thread “fundamental” java.lang.UnsupportedOperationException

        at java.util.concurrent.CopyOnWriteArrayList$COWIterator.take away(CopyOnWriteArrayList.java:1004)

        at check.CollectionTest.fundamental(CollectionTest.java:29)

Java Consequence: 1


Here’s a abstract of CopyOnWriteArrayList in a slide, it teaches you what all of the phrases have taught you thus far:

What is CopyOnWriteArrayList in Java

That is all about what’s CopyOnWriteArrayList, the distinction between CopyOnWriteArrayList and ArrayList in Java, and an instance of CopyOnWriteArrayList. Briefly, use CopyOnWriteArrayList for those who largely require to Iterate over the listing with out modifying it.

Additional Studying


Thanks for studying this text thus far, for those who like this text then please share with your pals and colleagues. If in case you have any suggestions or questions then please drop a word. 

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments