Queue.Synchronized(Queue) Metodo

Definizione

Restituisce un nuovo Queue oggetto che esegue il wrapping della coda originale ed è thread-safe.

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

Parametri

queue
Queue

Oggetto Queue da sincronizzare.

Valori restituiti

Wrapper Queue sincronizzato (thread-safe).

Eccezioni

queue è null.

Esempio

Nell'esempio di codice seguente viene illustrato come bloccare la raccolta usando SyncRoot durante l'intera enumerazione . Questo metodo è un'operazione 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

Nell'esempio seguente viene illustrato come sincronizzare un Queueoggetto , determinare se un Queue oggetto è sincronizzato e usare un oggetto sincronizzato 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.

Commenti

Il wrapper restituito da questo metodo blocca la coda prima dell'esecuzione di un'operazione in modo che venga eseguita in modo thread-safe.

Per garantire la thread safety di Queue, tutte le operazioni devono essere eseguite solo tramite questo wrapper.

L'enumerazione tramite una raccolta non è intrinsecamente una procedura thread-safe. Anche quando una raccolta viene sincronizzata, altri thread possono comunque modificare la raccolta, causando la generazione di un'eccezione da parte dell'enumeratore. Per garantire la thread safety durante l'enumerazione, è possibile bloccare la raccolta durante l'intera enumerazione o intercettare le eccezioni risultanti dalle modifiche apportate da altri thread.

Si applica a

Vedi anche