Hashtable.Synchronized(Hashtable) Metódus
Definíció
Fontos
Egyes információk olyan, kiadás előtti termékekre vonatkoznak, amelyek a kiadásig még jelentősen módosulhatnak. A Microsoft nem vállal kifejezett vagy törvényi garanciát az itt megjelenő információért.
Szinkronizált (szálbiztos) burkolót ad vissza a 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
Paraméterek
Válaszok
Szinkronizált (szálbiztos) burkoló a Hashtable.
Kivételek
table az null.
Példák
Az alábbi példa bemutatja, hogyan szinkronizálhat, Hashtableállapíthat meg egy Hashtable szinkronizálást, és hogyan használhat szinkronizáltatva Hashtable.
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.
Megjegyzések
A Synchronized módszer több olvasó és író számára is biztonságos. Emellett a szinkronizált burkoló biztosítja, hogy egyszerre csak egy író írjon.
A gyűjteményen keresztüli számbavétel alapvetően nem szálbiztos eljárás. A gyűjtemény szinkronizálása esetén is más szálak módosíthatják a gyűjteményt, ami miatt az enumerátor kivételt okoz. Az enumerálás során a szálbiztonság garantálása érdekében zárolhatja a gyűjteményt a teljes enumerálás során, vagy elkaphatja a más szálak által végrehajtott módosításokból eredő kivételeket.
Az alábbi példakód bemutatja, hogyan zárolhatja a gyűjteményt a SyncRoot teljes enumerálás során:
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
Ez a metódus egy O(1) művelet.