Stack.IsSynchronized プロパティ

定義

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

public:
 virtual property bool IsSynchronized { bool get(); };
public virtual bool IsSynchronized { get; }
member this.IsSynchronized : bool
Public Overridable ReadOnly Property IsSynchronized As Boolean

プロパティ値

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

実装

次の例は、 を同期 Stackし、 が同期されているかどうかを Stack 判断し、同期された Stackを使用する方法を示しています。

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

注釈

のスレッド セーフ Stackを保証するには、 メソッドによって返されるラッパーを介してすべての操作を Synchronized 実行する必要があります。

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

次のコード例は、 列挙体全体で を使用して SyncRoot コレクションをロックする方法を示しています。

Stack^ myCollection = gcnew Stack();
bool lockTaken = false;
try
{
    Monitor::Enter(myCollection->SyncRoot, lockTaken);
    for each (Object^ item in myCollection);
    {
        // Insert your code here.
    }
}
finally
{
    if (lockTaken)
    {
        Monitor::Exit(myCollection->SyncRoot);
    }
}
Stack myCollection = new Stack();

lock (myCollection.SyncRoot)
{
    foreach (object item in myCollection)
    {
        // Insert your code here.
    }
}
Dim myCollection As New Stack()

SyncLock myCollection.SyncRoot
    For Each item As Object In myCollection
        ' Insert your code here.
    Next item
End SyncLock

このプロパティの値を取得するのは操作です O(1)

適用対象