Queue.Synchronized(Queue) Metoda

Definice

Vrátí novou Queue , která zabalí původní frontu a je bezpečná pro vlákno.

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

Parametry

queue
Queue

Synchronizace:Queue

Návraty

Obálka Queue , která je synchronizovaná (bezpečné vlákno).

Výjimky

queue je null.

Příklady

Následující příklad kódu ukazuje, jak uzamknout kolekci pomocí celého výčtu SyncRoot . Tato metoda je O(1) operace.

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

Následující příklad ukazuje, jak synchronizovat Queue, určit, zda Queue je synchronizován a používat synchronizované 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.

Poznámky

Obálka vrácená touto metodou uzamkne frontu před provedením operace tak, aby byla provedena bezpečným způsobem.

Chcete-li zaručit bezpečnost Queuevlákna , všechny operace musí být provedeny pouze prostřednictvím tohoto obálky.

Výčet prostřednictvím kolekce není vnitřně bezpečným postupem pro přístup z více vláken. I když je kolekce synchronizována, ostatní vlákna mohou stále upravovat kolekci, což způsobí, že enumerátor vyvolá výjimku. Chcete-li zaručit bezpečnost vláken během výčtu, můžete buď uzamknout kolekci během celého výčtu, nebo zachytit výjimky vyplývající z změn provedených jinými vlákny.

Platí pro

Viz také