Để tạo một trình bao bọc được đồng bộ hóa cho ArrayList, mã như sau -
Ví dụ
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);
}
} Đầu ra
Điều này sẽ tạo ra kết quả sau -
ArrayList elements... AB CD EF GH IJ KL ArrayList is synchronized? = False
Ví dụ
Hãy để chúng tôi xem một ví dụ khác -
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);
}
} Đầu ra
Điều này sẽ tạo ra kết quả sau -
ArrayList elements... AB CD EF GH IJ KL ArrayList is synchronized? = False ArrayList is synchronized? = True