Queue.IsSynchronized 속성

정의

Queue 대한 액세스가 동기화되는지 여부를 나타내는 값을 가져옵니다(스레드로부터 안전).

public:
 virtual property bool IsSynchronized { bool get(); };
public virtual bool IsSynchronized { get; }
member this.IsSynchronized : bool
Public Overridable ReadOnly Property IsSynchronized As Boolean

속성 값

true 에 대한 액세스가 Queue 동기화되면(스레드로부터 안전함), 그렇지 않으면 false. 기본값은 false입니다.

구현

예제

다음 코드 예제에서는 전체 열거형 중에 컬렉션을 SyncRoot 잠그는 방법을 보여 있습니다. 이 속성의 값을 검색하는 작업은 작업입니다 O(1) .

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;
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안전을 보장하려면 메서드에서 반환 Synchronized 된 래퍼를 통해 모든 작업을 수행해야 합니다.

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

적용 대상

추가 정보