C# ArrayList - Synchronized() Method



The C# ArrayList Synchronized() method is used to create a synchronized (thread-safe) wrapper around an existing ArrayList. This method makes sure that all operations on the ArrayList are thread-safe when accessed by multiple threads.

Syntax

Following is the syntax of the C# ArrayList Synchronized() method −

public static ArrayList Synchronized(ArrayList list);

Parameters

This method accepts an ArrayList that needs to be synchronized.

Return value

This method Returns a thread-safe (synchronized) wrapper around the provided ArrayList.

Example 1: Create an ArrayList Thread-Safty

Following is the basic example of the Synchronized() method to make an ArrayList thread safty −

using System;
using System.Collections;
using System.Threading;

class Program
{
   static void Main()
   {
      ArrayList myList = new ArrayList() { "A", "B", "C" };

      // Creating a synchronized (thread-safe)
      ArrayList syncList = ArrayList.Synchronized(myList);

      Console.WriteLine("Original List:");
      // Locking ensure the thread safety
      lock (syncList.SyncRoot)
      {
         foreach (var item in syncList)
         {
            Console.Write(item + " ");
         }
      }
      Console.WriteLine();
   }
}

Output

Following is the output −

Original List:
A B C 

Example 2: Simulating Multi-Threaded Access

Here, in this example, we create an ArrayList. We then wrapped with thread-safe, and after that, we simulated with multithread to add elements in the ArrayList −

using System;
using System.Collections;
using System.Threading;
class Program
{
   static void Main()
   {
      ArrayList myList = new ArrayList() { "A", "B", "C" };

      // Creating a synchronized wrapper
      ArrayList syncList = ArrayList.Synchronized(myList);

      // Simulating multi-threaded access
      Thread thread1 = new Thread(() =>
      {
         lock (syncList.SyncRoot)
         {
            syncList.Add("D");
            Console.WriteLine("Thread 1 added D");
         }
      });

      Thread thread2 = new Thread(() =>
      {
          lock (syncList.SyncRoot)
          {
             syncList.Add("E");
             Console.WriteLine("Thread 2 added E");
          }
      });

      thread1.Start();
      thread2.Start();
      thread1.Join();
      thread2.Join();

      // Displaying the updated synchronized list
      Console.WriteLine("Updated List:");
      lock (syncList.SyncRoot)
      {
         foreach (var item in syncList)
         {
            Console.Write(item + " ");
         }
      }
   }
}

Output

Following is the output −

Thread 1 added D
Thread 2 added E
Updated List:
A B C D E

Example 3: Adding Elements in a Thread-Safe Manner

The following example creates an ArrayList and then adds the element in the ArrayList in a thread-safe manner. Use the Synchronized method to make the ArrayList thread-safe −

using System;
using System.Collections;
class Example
{
   static void Main()
   {
      ArrayList myList = new ArrayList() { 1, 2, 3, 4, 5 };

      // Creating a synchronized (thread-safe) wrapper
      ArrayList syncList = ArrayList.Synchronized(myList);

      // Adding elements in a thread-safe manner
      lock (syncList.SyncRoot)
      {
         syncList.Add(6);
         syncList.Add(7);
      }

      Console.WriteLine("Synchronized List:");
      lock (syncList.SyncRoot)
      {
         foreach (var item in syncList)
         {
            Console.Write(item + " ");
         }
      }
   }
}

Output

Following is the output −

Original List:
1 2 3 4 5 
Exception: Collection was of a fixed size.
Advertisements