Stack.Synchronized(Stack) 메서드

정의

에 대한 동기화된(스레드로부터 안전한) 래퍼를 반환합니다 Stack.

public:
 static System::Collections::Stack ^ Synchronized(System::Collections::Stack ^ stack);
public static System.Collections.Stack Synchronized(System.Collections.Stack stack);
static member Synchronized : System.Collections.Stack -> System.Collections.Stack
Public Shared Function Synchronized (stack As Stack) As Stack

매개 변수

stack
Stack

Stack 동기화할 수 있습니다.

반품

에 대한 동기화된 래퍼입니다 Stack.

예외

stacknull입니다.

예제

다음 예제에서는 동기화하고, 동기화 Stack되었는지 Stack 확인하고, 동기화된 Stack동기화를 사용하는 방법을 보여줍니다.

using System;
using System.Collections;

public class SamplesStack
{
    public static void Main()
    {
        // Creates and initializes a new Stack.
        Stack myStack = new Stack();
        myStack.Push("The");
        myStack.Push("quick");
        myStack.Push("brown");
        myStack.Push("fox");

        // Creates a synchronized wrapper around the Stack.
        Stack mySyncdStack = Stack.Synchronized(myStack);

        // Displays the sychronization status of both Stacks.
        Console.WriteLine("myStack is {0}.",
           myStack.IsSynchronized ? "synchronized" : "not synchronized");
        Console.WriteLine("mySyncdStack is {0}.",
           mySyncdStack.IsSynchronized ? "synchronized" : "not synchronized");
    }
}
/*
This code produces the following output.

myStack is not synchronized.
mySyncdStack is synchronized.
*/
Imports System.Collections

Public Class SamplesStack    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new Stack.
        Dim myStack As New Stack()
        myStack.Push("The")
        myStack.Push("quick")
        myStack.Push("brown")
        myStack.Push("fox")
        
        ' Creates a synchronized wrapper around the Stack.
        Dim mySyncdStack As Stack = Stack.Synchronized(myStack)

        ' Displays the sychronization status of both Stacks.
        Dim msg As String
        If myStack.IsSynchronized Then
            msg = "synchronized"
        Else
            msg = "not synchronized"
        End If        
        Console.WriteLine("myStack is {0}.", msg)        
        If mySyncdStack.IsSynchronized Then
            msg = "synchronized"
        Else
            msg = "not synchronized"
        End If
        Console.WriteLine("mySyncdStack is {0}.", msg)
    End Sub
End Class

' This code produces the following output.
' 
' myStack is not synchronized.
' mySyncdStack is synchronized.

설명

스레드의 Stack안전을 보장하려면 이 래퍼를 통해 모든 작업을 수행해야 합니다.

컬렉션을 열거하는 것은 본질적으로 스레드로부터 안전한 프로시저가 아닙니다. 컬렉션이 동기화된 경우에도 다른 스레드는 컬렉션을 수정할 수 있으므로 열거자가 예외를 throw합니다. 열거 중 스레드 안전을 보장하기 위해 전체 열거 중에 컬렉션을 잠그거나 다른 스레드의 변경으로 인한 예외를 catch할 수 있습니다.

다음 코드 예제에서는 전체 열거형 중에 컬렉션을 SyncRoot 잠그는 방법을 보여 있습니다.

Stack myCollection = new Stack();

lock (myCollection.SyncRoot)
{
    foreach (object item in myCollection)
    {
        // Insert your code here.
    }
}
Dim myCollection As New Stack()

SyncLock myCollection.SyncRoot
    For Each item As Object In myCollection
        ' Insert your code here.
    Next item
End SyncLock

이 메서드는 작업입니다 O(1) .

적용 대상