다음을 통해 공유


Hashtable.Synchronized 메서드

Hashtable에 대해 동기화되어 스레드로부터 안전하게 보호되는 래퍼를 반환합니다.

네임스페이스: System.Collections
어셈블리: mscorlib(mscorlib.dll)

구문

‘선언
Public Shared Function Synchronized ( _
    table As Hashtable _
) As Hashtable
‘사용 방법
Dim table As Hashtable
Dim returnValue As Hashtable

returnValue = Hashtable.Synchronized(table)
public static Hashtable Synchronized (
    Hashtable table
)
public:
static Hashtable^ Synchronized (
    Hashtable^ table
)
public static Hashtable Synchronized (
    Hashtable table
)
public static function Synchronized (
    table : Hashtable
) : Hashtable

매개 변수

반환 값

Hashtable에 대해 동기화되어 스레드로부터 안전하게 보호되는 래퍼입니다.

예외

예외 형식 조건

ArgumentNullException

table이 Null 참조(Visual Basic의 경우 Nothing)인 경우

설명

참고

이 메서드에 적용되는 HostProtectionAttribute 특성의 Resources 속성 값은 Synchronization입니다. HostProtectionAttribute는 대개 아이콘을 두 번 클릭하거나, 명령을 입력하거나, 브라우저에서 URL을 입력하여 시작되는 데스크톱 응용 프로그램에 영향을 미치지 않습니다. 자세한 내용은 HostProtectionAttribute 클래스나 SQL Server 프로그래밍 및 호스트 보호 특성을 참조하십시오.

SynchronizedHashtable을 읽고 있는 스레드가 없을 경우에 한해 여러 쓰기 스레드를 지원합니다. 하나 이상의 읽기 스레드와 하나 이상의 쓰기 스레드가 있는 경우에는 동기화된 래퍼에서 스레드로부터 안전한 액세스를 제공하지 않습니다.

컬렉션을 열거하는 프로시저는 기본적으로 스레드로부터 안전하지 않습니다. 컬렉션이 동기화되어 있을 때 다른 스레드에서 해당 컬렉션을 수정할 수 있으므로 이렇게 되면 열거자에서 예외가 throw됩니다. 열거하는 동안 스레드로부터 안전을 보장하려면 전체 열거를 수행하는 동안 컬렉션을 잠그거나 다른 스레드에서 변경된 내용으로 인해 발생한 예외를 catch하면 됩니다.

다음 코드 예제에서는 전체 열거 중에 SyncRoot를 사용하여 컬렉션을 잠그는 방법을 보여 줍니다.

Hashtable myCollection = new Hashtable();
  lock(myCollection.SyncRoot) {
  foreach (Object item in myCollection) {
  // Insert your code here.
  }
 }
Dim myCollection As New Hashtable()
 Dim item As Object
 SyncLock myCollection.SyncRoot
  For Each item In myCollection
  ' Insert your code here.
  Next item
 End SyncLock

이 메서드는 O(1) 연산입니다.

예제

다음 예제에서는 Hashtable을 동기화하는 방법, Hashtable이 동기화되었는지 여부를 확인하는 방법 및 동기화된 Hashtable을 사용하는 방법을 보여 줍니다.

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. 
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.
*/ 
#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.
 */
import System.*;
import System.Collections.*;

public class SamplesHashtable
{
    public static void main(String[] args)
    {
        // Creates and initializes a new Hashtable.
        Hashtable myHT = new Hashtable();

        myHT.Add(new Integer(0), "zero");
        myHT.Add(new Integer(1), "one");
        myHT.Add(new Integer(2), "two");
        myHT.Add(new Integer(3), "three");
        myHT.Add(new Integer(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.get_IsSynchronized()) ? 
            "synchronized" : "not synchronized");
        Console.WriteLine("mySyncdHT is {0}.", 
            (mySyncdHT.get_IsSynchronized()) ? 
            "synchronized" : "not synchronized");
    } //main
} //SamplesHashtable

/* 
 This code produces the following output.
 
 myHT is not synchronized.
 mySyncdHT is synchronized.
 */
import System
import System.Collections

// Creates and initializes a new Hashtable.
var myHT : Hashtable = 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.
var mySyncdHT : Hashtable = Hashtable.Synchronized(myHT)

// Displays the sychronization status of both Hashtables.
var msg : String
if(myHT.IsSynchronized)
    msg = "synchronized"
else
    msg = "not synchronized"
Console.WriteLine("myHT is {0}.", msg)
if(mySyncdHT.IsSynchronized)
    msg = "synchronized"
else
    msg = "not synchronized"
Console.WriteLine("mySyncdHT is {0}.", msg)

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

플랫폼

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0, 1.0에서 지원

참고 항목

참조

Hashtable 클래스
Hashtable 멤버
System.Collections 네임스페이스
IsSynchronized
SyncRoot