Synchronized method in ArrayList

In this article I will explain the Synchronized(ArrayList) method of ArrayList.
  • 2579

Introduction

The Synchronized(ArrayList) method of ArrayList returns a ArrayList wrapper that is synchronized (thread safe).We pass the ArrayList in the Synchronized method and then check that it is Synchronize or not by using IsSynchronized property of ArrayList,Which returns the result in the form of true and false,if it is Synchronized then it return true otherwise false.In the following example I use conditional statement.

Example

namespace ConsoleApplication6

{

    class Program

    {

        static void Main(string[] args)

        {

            // Creates a new ArrayList.

            ArrayList days = new ArrayList();

            days.Add("Sunday");

            days.Add("Monday");

            days.Add("Tuesday");

            days.Add("Wednesday");

            // Creates a Synchronized arraylist

            ArrayList synchro = ArrayList.Synchronized(days);

            // Displays whether the ArrayList is Synchronized or not

            Console.WriteLine("ArrayList days is {0}.", days.IsSynchronized ? "Synchronized" : "Not-Synchronized");

            Console.WriteLine("ArrayList synchro is {0}.", synchro.IsSynchronized ? "Synchronized" : "Not-Synchronized");

        }

    }

}

 

Output

synchronize.jpg 

 

Ask Your Question  

Got a programming related question? You may want to post your question here
 

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.