Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Stack.Synchronized() Method in C#
The Stack.Synchronized() method in C# returns a synchronized (thread-safe) wrapper for a Stack. This wrapper ensures that all operations on the stack are thread-safe, making it suitable for use in multi-threaded applications where multiple threads might access the same stack simultaneously.
Syntax
Following is the syntax for the Stack.Synchronized() method −
public static System.Collections.Stack Synchronized(System.Collections.Stack stack);
Parameters
stack − The Stack object to synchronize.
Return Value
Returns a synchronized wrapper for the specified Stack. The returned wrapper provides thread-safe access to all Stack operations.
Using Stack.Synchronized() Method
Example
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 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 the stack? = " + stack.Contains(5000));
Stack stack2 = (Stack)stack.Clone();
Console.WriteLine("\nStack elements...cloned");
foreach(int val in stack2) {
Console.WriteLine(val);
}
Console.WriteLine("Count of elements (cloned) = " + stack2.Count);
Console.WriteLine("Is the Stack synchronized? = " + stack.IsSynchronized);
Stack stack3 = Stack.Synchronized(stack);
Console.WriteLine("Is the Stack synchronized? = " + stack3.IsSynchronized);
}
}
The output of the above code is −
Stack elements... 2500 2000 1500 1250 1000 750 500 300 150 Count of elements = 9 Element 750 is the stack? = True Stack elements...updated 3000 2500 2000 1500 1250 1000 750 500 300 150 Count of elements (updated) = 10 Element 5000 is the stack? = False Stack elements...cloned 3000 2500 2000 1500 1250 1000 750 500 300 150 Count of elements (cloned) = 10 Is the Stack synchronized? = False Is the Stack synchronized? = True
Using Synchronized Wrapper with String Elements
Example
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);
}
}
The output of the above code is −
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
Key Points
-
The original stack remains unsynchronized − only the returned wrapper is thread-safe.
-
The
IsSynchronizedproperty returnsfalsefor the original stack andtruefor the synchronized wrapper. -
All operations on the synchronized wrapper are automatically thread-safe without additional locking.
-
The synchronized wrapper maintains the same functionality as the original stack.
Conclusion
The Stack.Synchronized() method creates a thread-safe wrapper around an existing Stack, enabling safe access from multiple threads. The original stack remains unchanged, while the returned wrapper provides synchronized access to all stack operations through its IsSynchronized property being true.
