Hashtable.Synchronized(Hashtable) Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Restituisce un wrapper sincronizzato (thread-safe) per .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
Parametri
Valori restituiti
Wrapper sincronizzato (thread-safe) per .Hashtable
Eccezioni
table è null.
Esempio
Nell'esempio seguente viene illustrato come sincronizzare un Hashtableoggetto , determinare se un oggetto Hashtable è sincronizzato e usare un oggetto sincronizzato 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.
Commenti
Il Synchronized metodo è thread-safe per più lettori e writer. Inoltre, il wrapper sincronizzato garantisce che sia presente un solo writer che scrive alla volta.
L'enumerazione tramite una raccolta non è intrinsecamente una procedura thread-safe. Anche quando una raccolta viene sincronizzata, altri thread possono comunque modificare la raccolta, causando la generazione di un'eccezione da parte dell'enumeratore. Per garantire la thread safety durante l'enumerazione, è possibile bloccare la raccolta durante l'intera enumerazione o intercettare le eccezioni risultanti dalle modifiche apportate da altri thread.
Nell'esempio di codice seguente viene illustrato come bloccare la raccolta usando SyncRoot durante l'intera enumerazione:
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
Questo metodo è un'operazione O(1) .