Stack.IsSynchronized プロパティ
アセンブリ: mscorlib (mscorlib.dll 内)


Stack を確実にスレッド セーフにするためには、すべての操作を Synchronized メソッドから返されるラッパー経由で実行する必要があります。
コレクションの列挙処理は、本質的にはスレッド セーフな処理ではありません。コレクションが同期されている場合でも、他のスレッドがそのコレクションを変更する可能性はあり、そのような状況が発生すると列挙子は例外をスローします。列挙処理を確実にスレッド セーフに行うには、列挙中にコレクションをロックするか、他のスレッドによって行われた変更によってスローされる例外をキャッチします。
列挙処理中に SyncRoot を使用してコレクションをロックする方法を次のコード例に示します。
Stack myCollection = new Stack(); lock(myCollection.SyncRoot) { foreach (Object item in myCollection) { // Insert your code here. } }

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

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- Stack.IsSynchronized プロパティのページへのリンク