Queue.Synchronized 方法
返回同步的(线程安全)Queue 包装。
**命名空间:**System.Collections
**程序集:**mscorlib(在 mscorlib.dll 中)
语法
声明
Public Shared Function Synchronized ( _
queue As Queue _
) As Queue
用法
Dim queue As Queue
Dim returnValue As Queue
returnValue = Queue.Synchronized(queue)
public static Queue Synchronized (
Queue queue
)
public:
static Queue^ Synchronized (
Queue^ queue
)
public static Queue Synchronized (
Queue queue
)
public static function Synchronized (
queue : Queue
) : Queue
参数
- queue
要同步的 Queue。
返回值
同步的(线程安全)Queue 包装。
异常
异常类型 | 条件 |
---|---|
queue 为 空引用(在 Visual Basic 中为 Nothing)。 |
备注
提示
应用于此方法的 HostProtectionAttribute 属性 (attribute) 拥有以下 Resources 属性 (property) 值:Synchronization。HostProtectionAttribute 不影响桌面应用程序(桌面应用程序一般通过双击图标,键入命令或在浏览器中输入 URL 启动)。有关更多信息,请参见 HostProtectionAttribute 类或 SQL Server 编程和宿主保护属性。
若要保证 Queue 的线程安全,必须通过此包装执行所有操作。
通过集合枚举在本质上不是一个线程安全的过程。即使一个集合已进行同步,其他线程仍可以修改该集合,这将导致枚举数引发异常。若要在枚举过程中保证线程安全,可以在整个枚举过程中锁定集合,或者捕捉由于其他线程进行的更改而引发的异常。
示例
下面的代码示例演示如何在整个枚举过程中使用 SyncRoot 锁定集合。此方法的运算复杂度为 O(1)。
Queue myCollection = new Queue();
lock(myCollection.SyncRoot) {
foreach (Object item in myCollection) {
// Insert your code here.
}
}
Dim myCollection As New Queue()
Dim item As Object
SyncLock myCollection.SyncRoot
For Each item In myCollection
' Insert your code here.
Next item
End SyncLock
下面的示例说明如何同步 Queue、如何确定 Queue 是否同步以及如何使用同步的 Queue。
Imports System
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.
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.
*/
#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.
*/
import System.*;
import System.Collections.*;
public class SamplesQueue
{
public static void main(String[] args)
{
// 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.get_IsSynchronized()) ? "synchronized" : "not synchronized");
Console.WriteLine("mySyncdQ is {0}.",(mySyncdQ.get_IsSynchronized()) ?
"synchronized" : "not synchronized");
} //main
} //SamplesQueue
/*
This code produces the following output.
myQ is not synchronized.
mySyncdQ is synchronized.
*/
平台
Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
请参见
参考
Queue 类
Queue 成员
System.Collections 命名空间
IsSynchronized
SyncRoot