Stack.IsSynchronized 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得值,這個值表示對 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)
。