Hashtable.Synchronized メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > Hashtable.Synchronized メソッドの意味・解説 

Hashtable.Synchronized メソッド

Hashtable 用の同期された (スレッド セーフな) ラッパー返します

名前空間: System.Collections
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

例外例外
例外種類条件

ArgumentNullException

tablenull 参照 (Visual Basic では Nothing) です。

解説解説
メモメモ

このメソッド適用される HostProtectionAttribute 属性Resources プロパティの値は、Synchronization です。HostProtectionAttribute は、デスクトップ アプリケーション (一般的にはアイコンダブルクリックコマンド入力、またはブラウザURL入力して起動するアプリケーション) には影響しません。詳細については、HostProtectionAttribute クラストピックまたは「SQL Server プログラミングホスト保護属性」を参照してください

Hashtable読み取るスレッド存在しない場合Synchronized複数書き込みスレッドサポートします1 つ上の読み取りスレッド1 つ上の書き込みスレッド存在する場合同期ラッパースレッド セーフアクセス提供しません。

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

列挙処理中に 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. 
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


このページでは「.NET Framework クラス ライブラリ リファレンス」からHashtable.Synchronized メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からHashtable.Synchronized メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からHashtable.Synchronized メソッドを検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「Hashtable.Synchronized メソッド」の関連用語

Hashtable.Synchronized メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Hashtable.Synchronized メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS