SortedList.Synchronized(SortedList) 方法

定义

返回 SortedList 对象的同步(线程安全)包装。

public:
 static System::Collections::SortedList ^ Synchronized(System::Collections::SortedList ^ list);
public static System.Collections.SortedList Synchronized (System.Collections.SortedList list);
static member Synchronized : System.Collections.SortedList -> System.Collections.SortedList
Public Shared Function Synchronized (list As SortedList) As SortedList

参数

list
SortedList

要同步的 SortedList 对象。

返回

SortedList

SortedList 对象的同步(线程安全)包装。

例外

listnull

示例

下面的代码示例演示如何在整个枚举过程中使用 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 ,必须仅通过此包装完成所有操作。

枚举整个集合本质上不是一个线程安全的过程。 即使某个集合已同步,其他线程仍可以修改该集合,这会导致枚举数引发异常。 若要确保枚举过程中的线程安全性,可以在整个枚举期间锁定集合,或者捕获由其他线程进行的更改所导致的异常。

适用于

另请参阅