ArrayList에 대한 동기화된 래퍼를 생성하기 위한 코드는 다음과 같습니다 -
예
using System;
using System.Collections;
public class Demo {
public static void Main() {
ArrayList arrList = new ArrayList();
arrList.Add("AB");
arrList.Add("CD");
arrList.Add("EF");
arrList.Add("GH");
arrList.Add("IJ");
arrList.Add("KL");
Console.WriteLine("ArrayList elements...");
foreach(string str in arrList) {
Console.WriteLine(str);
}
Console.WriteLine("ArrayList is synchronized? = "+arrList.IsSynchronized);
}
} 출력
이것은 다음과 같은 출력을 생성합니다 -
ArrayList elements... AB CD EF GH IJ KL ArrayList is synchronized? = False
예
다른 예를 살펴보겠습니다 -
using System;
using System.Collections;
public class Demo {
public static void Main() {
ArrayList arrList = new ArrayList();
arrList.Add("AB");
arrList.Add("CD");
arrList.Add("EF");
arrList.Add("GH");
arrList.Add("IJ");
arrList.Add("KL");
Console.WriteLine("ArrayList elements...");
foreach(string str in arrList) {
Console.WriteLine(str);
}
Console.WriteLine("ArrayList is synchronized? = "+arrList.IsSynchronized);
ArrayList arrList2 = ArrayList.Synchronized(arrList);
Console.WriteLine("ArrayList is synchronized? = "+arrList2.IsSynchronized);
}
} 출력
이것은 다음과 같은 출력을 생성합니다 -
ArrayList elements... AB CD EF GH IJ KL ArrayList is synchronized? = False ArrayList is synchronized? = True