ArrayList.IsSynchronized 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得值,這個值表示對 ArrayList 的存取是否同步 (安全執行緒)。
public:
virtual property bool IsSynchronized { bool get(); };
public virtual bool IsSynchronized { get; }
member this.IsSynchronized : bool
Public Overridable ReadOnly Property IsSynchronized As Boolean
屬性值
如果 true
的存取為同步 (安全執行緒),則為 ArrayList,否則為 false
。 預設為 false
。
實作
範例
下列程式代碼範例示範如何在整個列舉期間使用 SyncRoot 鎖定 集合。
ArrayList^ myCollection = gcnew ArrayList();
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);
}
}
ArrayList myCollection = new ArrayList();
lock(myCollection.SyncRoot)
{
foreach (object item in myCollection)
{
// Insert your code here.
}
}
Dim myCollection As New ArrayList()
SyncLock myCollection.SyncRoot
For Each item As Object In myCollection
' Insert your code here.
Next item
End SyncLock
擷取這個屬性的值是 O(1)
作業。
下列程式代碼範例示範如何同步處理 ,判斷 是否已ArrayList同步ArrayList處理 並使用同步ArrayList處理。
using namespace System;
using namespace System::Collections;
int main()
{
// Creates and initializes a new ArrayList instance.
ArrayList^ myAL = gcnew ArrayList;
myAL->Add( "The" );
myAL->Add( "quick" );
myAL->Add( "brown" );
myAL->Add( "fox" );
// Creates a synchronized wrapper around the ArrayList.
ArrayList^ mySyncdAL = ArrayList::Synchronized( myAL );
// Displays the sychronization status of both ArrayLists.
String^ szRes = myAL->IsSynchronized ? (String^)"synchronized" : "not synchronized";
Console::WriteLine( "myAL is {0}.", szRes );
String^ szSyncRes = mySyncdAL->IsSynchronized ? (String^)"synchronized" : "not synchronized";
Console::WriteLine( "mySyncdAL is {0}.", szSyncRes );
}
/*
This code produces the following output.
myAL is not synchronized.
mySyncdAL is synchronized.
*/
using System;
using System.Collections;
public class SamplesArrayList {
public static void Main() {
// Creates and initializes a new ArrayList.
ArrayList myAL = new ArrayList();
myAL.Add( "The" );
myAL.Add( "quick" );
myAL.Add( "brown" );
myAL.Add( "fox" );
// Creates a synchronized wrapper around the ArrayList.
ArrayList mySyncdAL = ArrayList.Synchronized( myAL );
// Displays the sychronization status of both ArrayLists.
Console.WriteLine( "myAL is {0}.", myAL.IsSynchronized ? "synchronized" : "not synchronized" );
Console.WriteLine( "mySyncdAL is {0}.", mySyncdAL.IsSynchronized ? "synchronized" : "not synchronized" );
}
}
/*
This code produces the following output.
myAL is not synchronized.
mySyncdAL is synchronized.
*/
Imports System.Collections
Public Class SamplesArrayList
Public Shared Sub Main()
' Creates and initializes a new ArrayList.
Dim myAL As New ArrayList()
myAL.Add("The")
myAL.Add("quick")
myAL.Add("brown")
myAL.Add("fox")
' Creates a synchronized wrapper around the ArrayList.
Dim mySyncdAL As ArrayList = ArrayList.Synchronized(myAL)
' Displays the sychronization status of both ArrayLists.
Dim str As String
If myAL.IsSynchronized Then
str = "synchronized"
Else
str = "not synchronized"
End If
Console.WriteLine("myAL is {0}.", str)
If mySyncdAL.IsSynchronized Then
str = "synchronized"
Else
str = "not synchronized"
End If
Console.WriteLine("mySyncdAL is {0}.", str)
End Sub
End Class
' This code produces the following output.
'
' myAL is not synchronized.
' mySyncdAL is synchronized.
備註
若要保證的 ArrayList線程安全性,所有作業都必須透過 方法傳 Synchronized 回的包裝函式來完成。
透過集合進行列舉在本質上並非安全執行緒程序。 即使集合經過同步化,其他的執行緒仍可修改該集合,使列舉值擲回例外狀況。 若要保證列舉過程的執行緒安全,您可以在整個列舉過程中鎖定集合,或攔截由其他執行緒的變更所造成的例外狀況。