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