次の方法で共有


Hashtable.IsSynchronized プロパティ

Hashtable へのアクセスが同期されている (スレッド セーフである) かどうかを示す値を取得します。

Public Overridable ReadOnly Property IsSynchronized As Boolean  _   Implements ICollection.IsSynchronized
[C#]
public virtual bool IsSynchronized {get;}
[C++]
public: __property virtual bool get_IsSynchronized();
[JScript]
public function get IsSynchronized() : Boolean;

プロパティ値

Hashtable へのアクセスが同期されている (スレッド セーフである) 場合は true 。それ以外の場合は false 。既定値は false です。

実装

ICollection.IsSynchronized

解説

Hashtable では、1 つの書き込み操作と複数の読み込み操作が同時に発生しても問題ありません。しかし、複数の書き込み操作をサポートするには、すべての操作が Synchronized メソッドから返されるラッパー経由で実行される必要があります。

コレクションの列挙処理は、本質的にはスレッド セーフな処理ではありません。コレクションが同期されている場合でも、他のスレッドがそのコレクションを変更する可能性はあり、そのような状況が発生すると列挙子は例外をスローします。列挙処理を確実にスレッド セーフに行うには、列挙中にコレクションをロックするか、他のスレッドによって行われた変更によってスローされる例外をキャッチする方法のいずれかを実行できます。

[Visual Basic, C#] 列挙処理中に SyncRoot を使用してコレクションをロックする方法を次のコード例に示します。

 
Hashtable myCollection = new Hashtable();
 lock( myCollection.SyncRoot ) {
 foreach ( Object item in myCollection ) {
 // Insert your code here.
 }
}
[Visual Basic] 
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

使用例

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. 

[C#] 
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.
*/ 

[C++] 
#using <mscorlib.dll>
#using <system.dll>

using namespace System;
using namespace System::Collections;

void main()  {
   // Creates and initializes a new Hashtable.
   Hashtable* myHT = new Hashtable();
   myHT->Add( __box(0), S"zero" );
   myHT->Add( __box(1), S"one" );
   myHT->Add( __box(2), S"two" );
   myHT->Add( __box(3), S"three" );
   myHT->Add( __box(4), S"four" );

   // Creates a synchronized wrapper around the Hashtable.
   Hashtable* mySyncdHT = Hashtable::Synchronized( myHT );

   // Displays the sychronization status of both Hashtables.
   Console::WriteLine( S"myHT is {0}.", myHT->IsSynchronized ? S"synchronized" : S"not synchronized" );
   Console::WriteLine( S"mySyncdHT is {0}.", mySyncdHT->IsSynchronized ? S"synchronized" : S"not synchronized" );
}

 /*
 This code produces the following output.

 myHT is not synchronized.
 mySyncdHT is synchronized.
 */

[JScript] 
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 NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

Hashtable クラス | Hashtable メンバ | System.Collections 名前空間 | Synchronized | SyncRoot