เมธอด Stack.Synchronized() ใน C# ใช้สำหรับสร้างวอร์ปเปอร์ (wrapper) ที่ปลอดภัยเธรด (thread-safe) สำหรับออบเจ็กต์ Stack ทำให้สามารถเข้าถึงจากหลายเธรดพร้อมกันได้อย่างปลอดภัย
ไวยากรณ์ (Syntax)
public static System.Collections.Stack Synchronized (System.Collections.Stack stack);
พารามิเตอร์ stack คือ Stack ที่ต้องการทำให้ปลอดภัยเธรด
ตัวอย่างที่ 1: Stack ของจำนวนเต็ม
using System;
using System.Collections;
public class Demo
{
public static void Main()
{
Stack stack = new Stack();
stack.Push(150);
stack.Push(300);
stack.Push(500);
stack.Push(750);
stack.Push(1000);
stack.Push(1250);
stack.Push(1500);
stack.Push(2000);
stack.Push(2500);
Console.WriteLine("Stack elements...");
foreach (int val in stack)
{
Console.WriteLine(val);
}
Console.WriteLine("Count of elements = " + stack.Count);
Console.WriteLine("Element 750 is in the stack? = " + stack.Contains(750));
stack.Push(3000);
Console.WriteLine("\nStack elements...updated");
foreach (int val in stack)
{
Console.WriteLine(val);
}
Console.WriteLine("Count of elements (updated) = " + stack.Count);
Console.WriteLine("Element 5000 is in the stack? = " + stack.Contains(5000));
Stack stack2 = (Stack)stack.Clone();
Console.WriteLine("\nStack elements...cloned");
foreach (int val in stack2)
{
Console.WriteLine(val);
}
Console.Write("Count of elements (updated) = " + stack2.Count);
Console.WriteLine("\nIs the Stack synchronized? = " + stack.IsSynchronized);
Stack stack3 = Stack.Synchronized(stack);
Console.WriteLine("Is the Stack synchronized? = " + stack3.IsSynchronized);
}
}
ผลลัพธ์
Stack elements... 2500 2000 1500 1250 1000 750 500 300 150 Count of elements = 9 Element 750 is in the stack? = True Stack elements...updated 3000 2500 2000 1500 1250 1000 750 500 300 150 Count of elements (updated) = 10 Element 5000 is in the stack? = False Stack elements...cloned 3000 2500 2000 1500 1250 1000 750 500 300 150 Count of elements (updated) = 10 Is the Stack synchronized? = False Is the Stack synchronized? = True
ตัวอย่างที่ 2: Stack ของสตริง
using System;
using System.Collections;
public class Demo
{
public static void Main()
{
Stack stack = new Stack();
stack.Push("Inspiron");
stack.Push("Alienware");
stack.Push("Projectors");
stack.Push("Monitors");
stack.Push("XPS");
stack.Push("Laptop");
stack.Push("Notebook");
Console.WriteLine("Stack elements...");
foreach (string val in stack)
{
Console.WriteLine(val);
}
Console.WriteLine("Count of elements = " + stack.Count);
stack.Push("Ultrabook");
stack.Push("Cameras");
stack.Push("Keyboards");
Console.WriteLine("\nStack elements...updated");
foreach (string val in stack)
{
Console.WriteLine(val);
}
Console.WriteLine("\nCount of elements (updated) = " + stack.Count);
Console.WriteLine("Is the Stack synchronized? = " + stack.IsSynchronized);
Stack stack2 = Stack.Synchronized(stack);
Console.WriteLine("Is the Stack synchronized? = " + stack2.IsSynchronized);
}
}
ผลลัพธ์
Stack elements... Notebook Laptop XPS Monitors Projectors Alienware Inspiron Count of elements = 7 Stack elements...updated Keyboards Cameras Ultrabook Notebook Laptop XPS Monitors Projectors Alienware Inspiron Count of elements (updated) = 10 Is the Stack synchronized? = False Is the Stack synchronized? = True
ข้อสังเกตสำคัญ
- Stack ปกติ ไม่ได้ปลอดภัยเธรด (
IsSynchronized = false) - ใช้
Stack.Synchronized(stack)เพื่อรับวอร์ปเปอร์ที่ปลอดภัยเธรด - วอร์ปเปอร์ที่ได้จะมี
IsSynchronized = true - การ Clone Stack ไม่ทำให้ปลอดภัยเธรด — ต้องใช้ Synchronized แยกต่างหาก
- ใน .NET Core/.NET 5+ แนะนำให้ใช้
ConcurrentStack<T>แทนสำหรับประสิทธิภาพที่ดีกว่า