Queue.Synchronized(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 包装。A Queue wrapper that is synchronized (thread safe).
例外
queue 为 null。queue is null.
示例
下面的代码示例演示如何使用在 SyncRoot 整个枚举过程中锁定集合。The following code example shows how to lock the collection using the SyncRoot during the entire enumeration. 此方法是一种 O(1) 操作。This method is an O(1) operation.
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 。The following example shows how to synchronize a Queue, determine if a Queue is synchronized and use a synchronized 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.
注解
此方法返回的包装器将在操作执行前锁定队列,以便以线程安全的方式执行此操作。The wrapper returned by this method locks the queue before an operation is performed so that it is performed in a thread-safe manner.
为了保证的线程安全 Queue ,所有操作都必须仅通过此包装器完成。To guarantee the thread safety of the Queue, all operations must be done through this wrapper only.
枚举整个集合本质上不是一个线程安全的过程。Enumerating through a collection is intrinsically not a thread-safe procedure. 即使某个集合已同步,其他线程仍可以修改该集合,这会导致枚举数引发异常。Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. 若要确保枚举过程中的线程安全性,可以在整个枚举期间锁定集合,或者捕获由其他线程进行的更改所导致的异常。To guarantee thread safety during enumeration, you can either lock the collection during the entire enumeration or catch the exceptions resulting from changes made by other threads.