ArrayList.Synchronized 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
傳回同步 (安全執行緒) 的清單包裝函式。
多載
Synchronized(ArrayList) |
傳回已同步 (安全執行緒) 的 ArrayList 包裝函式。 |
Synchronized(IList) |
傳回已同步 (安全執行緒) 的 IList 包裝函式。 |
Synchronized(ArrayList)
- 來源:
- ArrayList.cs
- 來源:
- ArrayList.cs
- 來源:
- ArrayList.cs
傳回已同步 (安全執行緒) 的 ArrayList 包裝函式。
public:
static System::Collections::ArrayList ^ Synchronized(System::Collections::ArrayList ^ list);
public static System.Collections.ArrayList Synchronized (System.Collections.ArrayList list);
static member Synchronized : System.Collections.ArrayList -> System.Collections.ArrayList
Public Shared Function Synchronized (list As ArrayList) As ArrayList
參數
傳回
已同步 (安全執行緒) 的 ArrayList 包裝函式。
例外狀況
list
為 null
。
範例
下列程式代碼範例示範如何在整個列舉期間使用 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(IList)
- 來源:
- ArrayList.cs
- 來源:
- ArrayList.cs
- 來源:
- ArrayList.cs
傳回已同步 (安全執行緒) 的 IList 包裝函式。
public:
static System::Collections::IList ^ Synchronized(System::Collections::IList ^ list);
public static System.Collections.IList Synchronized (System.Collections.IList list);
static member Synchronized : System.Collections.IList -> System.Collections.IList
Public Shared Function Synchronized (list As IList) As IList
參數
傳回
已同步 (安全執行緒) 的 IList 包裝函式。
例外狀況
list
為 null
。
範例
下列程式代碼範例示範如何在整個列舉期間使用 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線程安全性,所有作業都必須透過這個包裝函式來完成。
透過集合進行列舉在本質上並非安全執行緒程序。 即使集合經過同步化,其他的執行緒仍可修改該集合,使列舉值擲回例外狀況。 若要保證列舉過程的執行緒安全,您可以在整個列舉過程中鎖定集合,或攔截由其他執行緒的變更所造成的例外狀況。