ArrayList.IsSynchronized Property is used to get a value which indicate whether access to the ArrayList is synchronized (thread safe).
Syntax:
public virtual bool IsSynchronized { get; }
Return Value: This property returns the true if access to the ArrayList is synchronized (thread safe) otherwise it returns false. The default is false.
Below programs illustrate the use of above-discussed property:
Example 1:
using System;
using System.Collections;
class GFG {
public static void Main()
{
ArrayList myList = new ArrayList();
myList.Add( "A" );
myList.Add( "B" );
myList.Add( "C" );
myList.Add( "D" );
myList.Add( "E" );
myList.Add( "F" );
ArrayList smyList = ArrayList.Synchronized(myList);
Console.WriteLine( "myList is {0}." , myList.IsSynchronized ?
"Synchronized" : "Not Synchronized" );
Console.WriteLine( "smyList is {0}." , smyList.IsSynchronized ?
"Synchronized" : "Not Synchronized" );
}
}
|
Output:
myList is Not Synchronized.
smyList is Synchronized.
Example 2:
using System;
using System.Collections;
class GFG {
public static void Main()
{
ArrayList myList = new ArrayList();
myList.Add(1);
myList.Add(2);
myList.Add(3);
myList.Add(4);
myList.Add(5);
myList.Add(5);
Console.WriteLine(myList.IsSynchronized);
}
}
|
Note:
- Retrieving the value of this property is an O(1) operation.
- To guarantee the thread safety of the ArrayList, all operations must be done through the wrapper returned by the Synchronized method.
- Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception.
Reference: