次の方法で共有


Stack.IsSynchronized プロパティ

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

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;

プロパティ値

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

実装

ICollection.IsSynchronized

解説

Stack を確実にスレッド セーフにするためには、すべての操作を Synchronized メソッドから返されるラッパー経由で実行する必要があります。

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

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

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

使用例

[Visual Basic, C#, C++] 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.
 

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

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

using namespace System;
using namespace System::Collections;

int main()  {

   // Creates and initializes a new Stack.
   Stack* myStack = new Stack();
   myStack->Push( S"The" );
   myStack->Push( S"quick" );
   myStack->Push( S"brown" );
   myStack->Push( S"fox" );

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

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

/*
This code produces the following output.

myStack is not synchronized.
mySyncdStack is synchronized.
*/

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: 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

参照

Stack クラス | Stack メンバ | System.Collections 名前空間