Stack 用の同期された (スレッド セーフな) ラッパーを返します。
Public Shared Function Synchronized( _
ByVal stack As Stack _) As Stack
[C#]
public static Stack Synchronized(Stackstack);
[C++]
public: static Stack* Synchronized(Stack* stack);
[JScript]
public static function Synchronized(
stack : Stack) : Stack;
パラメータ
- stack
同期する Stack 。
戻り値
Stack 用の同期されたラッパー。
例外
例外の種類 | 条件 |
---|---|
ArgumentNullException | stack が null 参照 (Visual Basic では Nothing) です。 |
解説
Stack を確実にスレッド セーフにするためには、すべての操作をこのラッパー経由で実行する必要があります。
コレクションの列挙処理は、本質的にはスレッド セーフな処理ではありません。コレクションが同期されている場合でも、他のスレッドがそのコレクションを変更する可能性はあり、そのような状況が発生すると列挙子は例外をスローします。列挙処理を確実にスレッド セーフに行うには、列挙中にコレクションをロックするか、他のスレッドによって行われた変更によってスローされる例外をキャッチする方法のいずれかを実行できます。
[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