BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

How to synchronize ArrayList in java with example

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

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

Top Related Articles:

  1. OOPs in Java: Encapsulation, Inheritance, Polymorphism, Abstraction
  2. Examples of throws Keyword in Java
  3. How to sort Hashtable in java
  4. Constructor Overloading in Java with examples
  5. Java ArrayList addAll(int index, Collection c) Method example

Tags: Collections, Java-ArrayList

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

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 Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap