beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

How to synchronize ArrayList in java with example

By Chaitanya Singh | Filed Under: Java Collections

We have already discussed a bit about synchronization when we shared the tutorial on Vector vs ArrayList. As we are aware that ArrayList is non-synchronized and should not be used in multi-thread environment without explicit synchronization. This post is to discuss how to synchronize ArrayList in Java.

There are two ways to synchronize explicitly:

  1. Using Collections.synchronizedList() method
  2. Using thread-safe variant of ArrayList: CopyOnWriteArrayList

Example 1: Collections.synchronizedList() method for Synchronizing ArrayList

In this example we are using Collections.synchronizedList() method. The important point to note here is that iterator should be in synchronized block in this type of synchronization as shown in the below example.

package beginnersbook.com;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Collections;

public class Details {

   public static void main(String a[]){
       List<String> syncal = 
         Collections.synchronizedList(new ArrayList<String>());

       //Adding elements to synchronized ArrayList
       syncal.add("Pen");
       syncal.add("NoteBook");
       syncal.add("Ink");

       System.out.println("Iterating synchronized ArrayList:");
       synchronized(syncal) {
       Iterator<String> iterator = syncal.iterator(); 
       while (iterator.hasNext())
          System.out.println(iterator.next());
       }
   }
}

Output:

Iterating synchronized ArrayList:
Pen
NoteBook
Ink

Method 2: Using CopyOnWriteArrayList

CopyOnWriteArrayList is a thread-safe variant of ArrayList.

package beginnersbook.com;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.Iterator;

public class Details {

 public static void main(String a[]){
    CopyOnWriteArrayList<String> al = new CopyOnWriteArrayList<String>();

    //Adding elements to synchronized ArrayList
    al.add("Pen");
    al.add("NoteBook");
    al.add("Ink");

    System.out.println("Displaying synchronized ArrayList Elements:");
    //Synchronized block is not required in this method
    Iterator<String> iterator = al.iterator(); 
    while (iterator.hasNext())
       System.out.println(iterator.next());
  }
}

Output:

Displaying synchronized ArrayList Elements:
Pen
NoteBook
Ink

Enjoyed this post? Try these related posts

  1. Queue Interface in Java Collections
  2. Java – Remove mapping from HashMap example
  3. How to clone an ArrayList to another ArrayList
  4. Hashtable in java with example
  5. Remove mapping from Hashtable example – Java
  6. Difference between ArrayList and Vector In java

Comments

  1. Ankur says

    February 5, 2016 at 6:57 PM

    Hi,
    In second method I am unable to import the package. It ask me to remove argument type from CopyOnWriteArrayList.

    Execution error below:
    The type CopyOnWriteArrayList is not generic; it cannot be parameterized with arguments .
    Could you please help here.

    Reply
  2. bk says

    July 6, 2016 at 9:24 AM

    Hi,in First example when i replace
    List syncal = Collections.synchronizedList(new ArrayList());
    with List syncal = new ArrayList(); the output still remain same.

    how can we distinguish the use of Thread safety.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

  • Java Tutorial
  • OOPs Concepts

Java Collections

  • ArrayList
  • LinkedList
  • ArrayList vs LinkedList
  • Vector
  • ArrayList vs Vector
  • HashMap
  • TreeMap
  • LinkedHashMap
  • HashSet
  • TreeSet
  • LinkedHashSet
  • Hashtable
  • HashMap vs Hashtable
  • Queue
  • PriorityQueue
  • Deque & ArrayDeque
  • Iterator
  • ListIterator
  • Comparable Interface
  • Comparator Interface
  • Java Collections Interview Q

MORE ...

  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap