Hashtable.Synchronized(Hashtable) Metoda

Definice

Vrátí synchronizovanou obálku (s bezpečným vláknem Hashtable) pro .

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

Parametry

table
Hashtable

Synchronizace:Hashtable

Návraty

Synchronizovaný (závitově bezpečný) obálka Hashtablepro .

Výjimky

table je null.

Příklady

Následující příklad ukazuje, jak synchronizovat Hashtable, určit, zda Hashtable je synchronizován, a použít synchronizovaný 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.

Poznámky

Metoda Synchronized je bezpečná pro více čtenářů a zapisovačů. Synchronizovaný obálka navíc zajišťuje, že najednou existuje jen jeden zápis zapisovače.

Výčet prostřednictvím kolekce není vnitřně bezpečným postupem pro přístup z více vláken. I když je kolekce synchronizována, ostatní vlákna mohou stále upravovat kolekci, což způsobí, že enumerátor vyvolá výjimku. Chcete-li zaručit bezpečnost vláken během výčtu, můžete buď uzamknout kolekci během celého výčtu, nebo zachytit výjimky vyplývající z změn provedených jinými vlákny.

Následující příklad kódu ukazuje, jak uzamknout kolekci pomocí SyncRoot celého výčtu:

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

Tato metoda je O(1) operace.

Platí pro

Viz také