Hashtable.IsSynchronized Hashtable.IsSynchronized Hashtable.IsSynchronized Hashtable.IsSynchronized Property

定义

获取一个值,该值指示是否同步对 Hashtable 的访问(线程安全)。Gets a value indicating whether access to the Hashtable is synchronized (thread safe).

public:
 virtual property bool IsSynchronized { bool get(); };
public virtual bool IsSynchronized { get; }
member this.IsSynchronized : bool
Public Overridable ReadOnly Property IsSynchronized As Boolean

属性值

如果对true 的访问是同步的(线程安全),则为 Hashtable ;否则为 falsetrue if access to the Hashtable is synchronized (thread safe); otherwise, false. 默认值为 falseThe default is false.

实现

示例

下面的示例演示如何同步Hashtable, 确定Hashtable是否同步, 以及是否使用同步Hashtable的。The following example shows how to synchronize a Hashtable, determine if a Hashtable is synchronized, and use a synchronized 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 SamplesHashtable
{

   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. 

注解

Hashtable可以同时支持一个编写器和多个读取器。A Hashtable can support one writer and multiple readers concurrently. 若要支持多个编写器, 必须通过Synchronized方法返回的包装完成所有操作。To support multiple writers, all operations must be done through the wrapper returned by the Synchronized method.

枚举集合本质上不是线程安全的过程。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.

下面的代码示例演示如何使用SyncRoot在整个枚举过程中锁定集合:The following code example shows how to lock the collection using the SyncRoot during the entire enumeration:

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

适用于

另请参阅