Hashtable.Synchronized 方法
返回 Hashtable 的同步(线程安全)包装。
**命名空间:**System.Collections
**程序集:**mscorlib(在 mscorlib.dll 中)
语法
声明
Public Shared Function Synchronized ( _
table As Hashtable _
) As Hashtable
用法
Dim table As Hashtable
Dim returnValue As Hashtable
returnValue = Hashtable.Synchronized(table)
public static Hashtable Synchronized (
Hashtable table
)
public:
static Hashtable^ Synchronized (
Hashtable^ table
)
public static Hashtable Synchronized (
Hashtable table
)
public static function Synchronized (
table : Hashtable
) : Hashtable
参数
- table
要同步的 Hashtable。
返回值
Hashtable 的同步(线程安全)包装。
异常
异常类型 | 条件 |
---|---|
table 为 空引用(在 Visual Basic 中为 Nothing)。 |
备注
提示
应用于此方法的 HostProtectionAttribute 属性 (Attribute) 的 Resources 属性 (Property) 值如下:Synchronization。HostProtectionAttribute 不影响桌面应用程序(桌面应用程序一般通过双击图标,键入命令或在浏览器中输入 URL 启动)。有关更多信息,请参见 HostProtectionAttribute 类或 SQL Server 编程和宿主保护属性。
如果没有任何线程在读取 Hashtable,则 Synchronized 支持使用多个写入线程。如果使用一个或多个读取器以及一个或多个编写器,则同步包装不提供线程安全的访问。
从头到尾对一个集合进行枚举本质上并不是一个线程安全的过程。即使一个集合已进行同步,其他线程仍可以修改该集合,这将导致枚举数引发异常。若要在枚举过程中保证线程安全,可以在整个枚举过程中锁定集合,或者捕捉由于其他线程进行的更改而引发的异常。
下面的代码示例显示如何在整个枚举过程中使用 SyncRoot 锁定集合:
Hashtable myCollection = new Hashtable();
lock(myCollection.SyncRoot) {
foreach (Object item in myCollection) {
// Insert your code here.
}
}
Dim myCollection As New Hashtable()
Dim item As Object
SyncLock myCollection.SyncRoot
For Each item In myCollection
' Insert your code here.
Next item
End SyncLock
此方法是 O(1) 操作。
示例
下面的示例演示如何同步 Hashtable、如何确定 Hashtable 是否同步以及如何使用同步的 Hashtable。
Imports System
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 Then
msg = "synchronized"
Else
msg = "not synchronized"
End If
Console.WriteLine("myHT is {0}.", msg)
If mySyncdHT.IsSynchronized Then
msg = "synchronized"
Else
msg = "not synchronized"
End If
Console.WriteLine("mySyncdHT is {0}.", msg)
End Sub
End Class
' This code produces the following output.
'
' myHT is not synchronized.
' mySyncdHT is synchronized.
using System;
using System.Collections;
public class SamplesHashtable {
public static void Main() {
// Creates and initializes a new Hashtable.
Hashtable 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.
*/
#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.
*/
import System.*;
import System.Collections.*;
public class SamplesHashtable
{
public static void main(String[] args)
{
// Creates and initializes a new Hashtable.
Hashtable myHT = new Hashtable();
myHT.Add(new Integer(0), "zero");
myHT.Add(new Integer(1), "one");
myHT.Add(new Integer(2), "two");
myHT.Add(new Integer(3), "three");
myHT.Add(new Integer(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.get_IsSynchronized()) ?
"synchronized" : "not synchronized");
Console.WriteLine("mySyncdHT is {0}.",
(mySyncdHT.get_IsSynchronized()) ?
"synchronized" : "not synchronized");
} //main
} //SamplesHashtable
/*
This code produces the following output.
myHT is not synchronized.
mySyncdHT is synchronized.
*/
import System
import System.Collections
// Creates and initializes a new Hashtable.
var myHT : Hashtable = 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.
var mySyncdHT : Hashtable = Hashtable.Synchronized(myHT)
// Displays the sychronization status of both Hashtables.
var msg : String
if(myHT.IsSynchronized)
msg = "synchronized"
else
msg = "not synchronized"
Console.WriteLine("myHT is {0}.", msg)
if(mySyncdHT.IsSynchronized)
msg = "synchronized"
else
msg = "not synchronized"
Console.WriteLine("mySyncdHT is {0}.", msg)
// This code produces the following output.
//
// myHT is not synchronized.
// mySyncdHT is synchronized.
平台
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、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
.NET Compact Framework
受以下版本支持:2.0、1.0
请参见
参考
Hashtable 类
Hashtable 成员
System.Collections 命名空间
IsSynchronized
SyncRoot