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

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

Stack.Synchronized メソッド

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

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

例外例外
例外種類条件

ArgumentNullException

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

解説解説
メモメモ

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

Stack確実にスレッド セーフにするためには、すべての操作をこのラッパー経由実行する必要があります

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

列挙処理中に SyncRoot を使用してコレクションロックする方法次のコード例示します

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

このメソッドは O(1) 操作です。

使用例使用例

Stack同期する方法と、Stack同期されているかどうか確認し同期済みStack使用する方法の例を次に示します

Imports System
Imports System.Collections

Public Class SamplesStack    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new Stack.
        Dim myStack As New
 Stack()
        myStack.Push("The")
        myStack.Push("quick")
        myStack.Push("brown")
        myStack.Push("fox")
        
        ' Creates a synchronized wrapper around the Stack.
        Dim mySyncdStack As Stack = Stack.Synchronized(myStack)

        ' Displays the sychronization status of both Stacks.
        Dim msg As String
        If myStack.IsSynchronized Then
            msg = "synchronized"
        Else
            msg = "not synchronized"
        End If        
        Console.WriteLine("myStack is {0}.", msg)
        
        If mySyncdStack.IsSynchronized Then
            msg = "synchronized"
        Else
            msg = "not synchronized"
        End If
        Console.WriteLine("mySyncdStack is {0}.",
 msg)
    End Sub
End Class

' This code produces the following output.
' 
' myStack is not synchronized.
' mySyncdStack is synchronized.
 
using System;
 using System.Collections;
 public class SamplesStack  {
 
    public static void Main()
  {
 
       // Creates and initializes a new Stack.
       Stack myStack = new Stack();
       myStack.Push( "The" );
       myStack.Push( "quick" );
       myStack.Push( "brown" );
       myStack.Push( "fox" );
 
       // Creates a synchronized wrapper around the Stack.
       Stack mySyncdStack = Stack.Synchronized( myStack );
 
       // Displays the sychronization status of both Stacks.
       Console.WriteLine( "myStack is {0}.",
          myStack.IsSynchronized ? "synchronized" : "not synchronized"
 );
       Console.WriteLine( "mySyncdStack is {0}.",
          mySyncdStack.IsSynchronized ? "synchronized" : "not synchronized"
 );
    }
 }
 /* 
 This code produces the following output.
 
 myStack is not synchronized.
 mySyncdStack is synchronized.
 */ 
#using <system.dll>

using namespace System;
using namespace System::Collections;
int main()
{
   
   // Creates and initializes a new Stack.
   Stack^ myStack = gcnew Stack;
   myStack->Push( "The" );
   myStack->Push( "quick" );
   myStack->Push( "brown" );
   myStack->Push( "fox" );
   
   // Creates a synchronized wrapper around the Stack.
   Stack^ mySyncdStack = Stack::Synchronized( myStack );
   
   // Displays the sychronization status of both Stacks.
   Console::WriteLine( "myStack is {0}.", myStack->IsSynchronized ?
 (String^)"synchronized" : "not synchronized" );
   Console::WriteLine( "mySyncdStack is {0}.", mySyncdStack->IsSynchronized
 ? (String^)"synchronized" : "not synchronized" );
}

/*
This code produces the following output.

myStack is not synchronized.
mySyncdStack is synchronized.
*/
import System.*;
import System.Collections.*;

public class SamplesStack
{
    public static void main(String[]
 args)
    {
        // Creates and initializes a new Stack.
        Stack myStack = new Stack();

        myStack.Push("The");
        myStack.Push("quick");
        myStack.Push("brown");
        myStack.Push("fox");

        // Creates a synchronized wrapper around the Stack.
        Stack mySyncdStack = Stack.Synchronized(myStack);

        // Displays the sychronization status of both Stacks.
        Console.WriteLine("myStack is {0}.", 
            (myStack.get_IsSynchronized()) ? 
            "synchronized" : "not synchronized");
        Console.WriteLine("mySyncdStack is {0}.", 
            (mySyncdStack.get_IsSynchronized()) ? 
            "synchronized" : "not synchronized");
    } //main

} //SamplesStack

/* 
 This code produces the following output.
 
 myStack is not synchronized.
 mySyncdStack is synchronized.
 */
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



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

辞書ショートカット

すべての辞書の索引

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

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

   

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



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

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

©2024 GRAS Group, Inc.RSS