Hashtable.Synchronized(Hashtable) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回 Hashtable 的同步(线程安全)包装。
public:
static System::Collections::Hashtable ^ Synchronized(System::Collections::Hashtable ^ table);
public static System.Collections.Hashtable Synchronized (System.Collections.Hashtable table);
static member Synchronized : System.Collections.Hashtable -> System.Collections.Hashtable
Public Shared Function Synchronized (table As Hashtable) As Hashtable
参数
返回
Hashtable 的同步(线程安全)包装。
例外
table
为 null
。
示例
以下示例演示如何同步 , Hashtable确定 是否 Hashtable 同步,并使用同步的 Hashtable。
#using <system.dll>
using namespace System;
using namespace System::Collections;
void main()
{
// Creates and initializes a new Hashtable.
Hashtable^ myHT = gcnew Hashtable;
myHT->Add( (int^)0, "zero" );
myHT->Add( 1, "one" );
myHT->Add( 2, "two" );
myHT->Add( 3, "three" );
myHT->Add( 4, "four" );
// Creates a synchronized wrapper around the Hashtable.
Hashtable^ mySyncdHT = Hashtable::Synchronized( myHT );
// Displays the sychronization status of both Hashtables.
Console::WriteLine( "myHT is {0}.", myHT->IsSynchronized ? (String^)"synchronized" : "not synchronized" );
Console::WriteLine( "mySyncdHT is {0}.", mySyncdHT->IsSynchronized ? (String^)"synchronized" : "not synchronized" );
}
/*
This code produces the following output.
myHT is not synchronized.
mySyncdHT is synchronized.
*/
using System;
using System.Collections;
public class SamplesHashtable2
{
public static void Main()
{
// Creates and initializes a new Hashtable.
var myHT = new Hashtable();
myHT.Add(0, "zero");
myHT.Add(1, "one");
myHT.Add(2, "two");
myHT.Add(3, "three");
myHT.Add(4, "four");
// Creates a synchronized wrapper around the Hashtable.
Hashtable mySyncdHT = Hashtable.Synchronized(myHT);
// Displays the sychronization status of both Hashtables.
Console.WriteLine("myHT is {0}.", myHT.IsSynchronized ? "synchronized" : "not synchronized");
Console.WriteLine("mySyncdHT is {0}.", mySyncdHT.IsSynchronized ? "synchronized" : "not synchronized");
}
}
/*
This code produces the following output.
myHT is not synchronized.
mySyncdHT is synchronized.
*/
Imports System.Collections
Public Class SamplesHashtable
Public Shared Sub Main()
' Creates and initializes a new Hashtable.
Dim myHT As New Hashtable()
myHT.Add(0, "zero")
myHT.Add(1, "one")
myHT.Add(2, "two")
myHT.Add(3, "three")
myHT.Add(4, "four")
' Creates a synchronized wrapper around the Hashtable.
Dim mySyncdHT As Hashtable = Hashtable.Synchronized(myHT)
' Displays the sychronization status of both Hashtables.
Dim msg As String = If(myHT.IsSynchronized, "synchronized", "not synchronized")
Console.WriteLine($"myHT is {msg}.")
msg = If(mySyncdHT.IsSynchronized, "synchronized", "not synchronized")
Console.WriteLine($"mySyncdHT is {msg}.")
End Sub
End Class
' This code produces the following output.
'
' myHT is not synchronized.
' mySyncdHT is synchronized.
注解
方法 Synchronized 对多个读取器和编写器是线程安全的。 此外,同步包装器可确保一次只有一个写入器写入。
枚举整个集合本质上不是一个线程安全的过程。 即使某个集合已同步,其他线程仍可以修改该集合,这会导致枚举数引发异常。 若要确保枚举过程中的线程安全性,可以在整个枚举期间锁定集合,或者捕获由其他线程进行的更改所导致的异常。
下面的代码示例演示如何在整个枚举期间使用 SyncRoot 锁定集合:
Hashtable^ myCollection = gcnew Hashtable();
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);
}
}
var myCollection = new Hashtable();
lock (myCollection.SyncRoot)
{
foreach (object item in myCollection)
{
// Insert your code here.
}
}
Dim myCollection As New Hashtable()
SyncLock myCollection.SyncRoot
For Each item In myCollection
' Insert your code here.
Next
End SyncLock
此方法是一种 O(1)
操作。