Queue.Synchronized(Queue) 메서드

정의

원래 큐를 래핑하고 스레드로부터 안전한 새 Queue를 반환합니다.

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

매개 변수

queue
Queue

동기화할 Queue입니다.

반환

동기화되어 스레드로부터 안전하게 보호되는 Queue 래퍼입니다.

예외

queue이(가) null인 경우

예제

다음 코드 예제에서는 전체 열거 중에 을 SyncRoot 사용하여 컬렉션을 잠그는 방법을 보여줍니다. 이 메서드는 작업입니다 O(1) .

Queue^ myCollection = gcnew Queue();
bool lockTaken = false;
try
{
    Monitor::Enter(myCollection->SyncRoot, lockTaken);
    for each (Object^ item in myCollection);
    {
        // Insert your code here.
    }
}
finally
{
    if (lockTaken)
    {
        Monitor::Exit(myCollection->SyncRoot);
    }
}
Queue myCollection = new Queue();
lock (myCollection.SyncRoot)
{
    foreach (object item in myCollection)
    {
        // Insert your code here.
    }
}
Dim myCollection As New Queue()
SyncLock myCollection.SyncRoot
    For Each item In myCollection
        ' Insert your code here.
    Next item
End SyncLock

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

#using <system.dll>

using namespace System;
using namespace System::Collections;
int main()
{
   
   // Creates and initializes a new Queue.
   Queue^ myQ = gcnew Queue;
   myQ->Enqueue( "The" );
   myQ->Enqueue( "quick" );
   myQ->Enqueue( "brown" );
   myQ->Enqueue( "fox" );
   
   // Creates a synchronized wrapper around the Queue.
   Queue^ mySyncdQ = Queue::Synchronized( myQ );
   
   // Displays the sychronization status of both Queues.
   Console::WriteLine( "myQ is {0}.", myQ->IsSynchronized ? (String^)"synchronized" : "not synchronized" );
   Console::WriteLine( "mySyncdQ is {0}.", mySyncdQ->IsSynchronized ? (String^)"synchronized" : "not synchronized" );
}

/*
This code produces the following output.

myQ is not synchronized.
mySyncdQ is synchronized.
*/
using System;
using System.Collections;

public class SamplesQueue
{
    public static void Main()
    {
        // Creates and initializes a new Queue.
        Queue myQ = new Queue();
        myQ.Enqueue("The");
        myQ.Enqueue("quick");
        myQ.Enqueue("brown");
        myQ.Enqueue("fox");

        // Creates a synchronized wrapper around the Queue.
        Queue mySyncdQ = Queue.Synchronized(myQ);

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

myQ is not synchronized.
mySyncdQ is synchronized.
*/
Imports System.Collections

Public Class SamplesQueue    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new Queue.
        Dim myQ As New Queue()
        myQ.Enqueue("The")
        myQ.Enqueue("quick")
        myQ.Enqueue("brown")
        myQ.Enqueue("fox")
        
        ' Creates a synchronized wrapper around the Queue.
        Dim mySyncdQ As Queue = Queue.Synchronized(myQ)
        
        ' Displays the sychronization status of both Queues.
        Dim msg As String
        If myQ.IsSynchronized Then
            msg = "synchronized"
        Else
            msg = "not synchronized"
        End If
        Console.WriteLine("myQ is {0}.", msg)
        If mySyncdQ.IsSynchronized Then
            msg = "synchronized"
        Else
            msg = "not synchronized"
        End If
        Console.WriteLine("mySyncdQ is {0}.", msg)
    End Sub
End Class

' This code produces the following output.
' 
' myQ is not synchronized.
' mySyncdQ is synchronized.

설명

이 메서드에서 반환된 래퍼는 스레드로부터 안전한 방식으로 수행되도록 작업을 수행하기 전에 큐를 잠급니다.

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

컬렉션 전체를 열거하는 프로시저는 기본적으로 스레드로부터 안전하지 않습니다. 컬렉션이 동기화되어 있을 때 다른 스레드에서 해당 컬렉션을 수정할 수 있으므로 이렇게 되면 열거자에서 예외가 throw됩니다. 열거하는 동안 스레드로부터 안전을 보장하려면 전체 열거를 수행하는 동안 컬렉션을 잠그거나 다른 스레드에서 변경된 내용으로 인해 발생한 예외를 catch하면 됩니다.

적용 대상

추가 정보