Hashtable.Synchronized(Hashtable) Hashtable.Synchronized(Hashtable) Hashtable.Synchronized(Hashtable) Hashtable.Synchronized(Hashtable) Method

정의

Hashtable에 대해 동기화되어 스레드로부터 안전하게 보호되는 래퍼를 반환합니다.Returns a synchronized (thread-safe) wrapper for the 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
Hashtable Hashtable Hashtable Hashtable

동기화할 Hashtable입니다.The Hashtable to synchronize.

반환

Hashtable에 대해 동기화되어 스레드로부터 안전하게 보호되는 래퍼입니다.A synchronized (thread-safe) wrapper for the Hashtable.

예외

예제

다음 예제에서는 동기화 하는 방법을 보여 줍니다는 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.
      Hashtable 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
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 Then
            msg = "synchronized"
        Else
            msg = "not synchronized"
        End If
        Console.WriteLine("myHT is {0}.", msg)
        If mySyncdHT.IsSynchronized Then
            msg = "synchronized"
        Else
            msg = "not synchronized"
        End If        
        Console.WriteLine("mySyncdHT is {0}.", msg)
    End Sub
End Class

' This code produces the following output.
' 
' myHT is not synchronized.
' mySyncdHT is synchronized. 

설명

Synchronized 메서드는 스레드로부터 안전 하 게 여러 판독기와 작성기에 대 한 합니다.The Synchronized method is thread safe for multiple readers and writers. 또한 동기화 된 래퍼 작성 한 번에 하나의 기록기 있는지 확인 합니다.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. 컬렉션이 동기화되어 있을 때 다른 스레드에서 해당 컬렉션을 수정할 수 있으므로 이렇게 되면 열거자에서 예외가 throw됩니다.Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception. 열거하는 동안 스레드로부터 안전을 보장하려면 전체 열거를 수행하는 동안 컬렉션을 잠그거나 다른 스레드에서 변경된 내용으로 인해 발생한 예외를 catch하면 됩니다.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);
    }
}
Hashtable 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

이 방법은 O(1) 작업에 설명 합니다.This method is an O(1) operation.

적용 대상

추가 정보