Queue.Synchronized(Queue) Methode
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Gibt eine neue Queue zurück, die die ursprüngliche Warteschlange umschließt und threadsicher ist.
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
Parameter
Gibt zurück
Ein Queue-Wrapper, der synchronisiert (threadsicher) ist.
Ausnahmen
queue ist null.
Beispiele
Im folgenden Codebeispiel wird gezeigt, wie Sie die Auflistung mithilfe SyncRoot der gesamten Enumeration sperren. Diese Methode ist ein O(1) Vorgang.
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
Im folgenden Beispiel wird gezeigt, wie Sie eine QueueSynchronisierung durchführen, bestimmen, ob eine Queue synchronisiert wird und eine synchronisierte QueueVerwendung verwendet.
#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.
Hinweise
Der von dieser Methode zurückgegebene Wrapper sperrt die Warteschlange, bevor ein Vorgang ausgeführt wird, sodass er auf eine threadsichere Weise ausgeführt wird.
Um die Threadsicherheit des QueueThreads zu gewährleisten, müssen alle Vorgänge nur über diesen Wrapper ausgeführt werden.
Die Enumeration einer Auflistung ist systemintern keine threadsichere Prozedur. Selbst wenn eine Auflistung synchronisiert wird, besteht die Möglichkeit, dass andere Threads sie ändern. Dies führt dazu, dass der Enumerator eine Ausnahme auslöst. Um während der Enumeration Threadsicherheit zu gewährleisten, können Sie entweder die Auflistung während der gesamten Enumeration sperren oder die Ausnahmen, die aus von anderen Threads stammenden Änderungen resultieren, abfangen.