Hashtable.Synchronized(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
パラメーター
戻り値
例外
table
が null
です。table
is null
.
例
次の例では、@no__t を同期し、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.
注釈
@No__t 0 のメソッドは、複数のリーダーとライターのスレッドセーフです。The Synchronized method is thread safe for multiple readers and writers. さらに、同期されたラッパーは、一度に1つのライター書き込みだけを行うことを保証します。Furthermore, the synchronized wrapper ensures that there is only one writer writing at a time.
コレクションの列挙は、本質的にスレッド セーフなプロシージャではありません。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
このメソッドは @no__t 0 演算です。This method is an O(1)
operation.