ArrayList.Synchronized 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
동기화된 목록 래퍼를 반환합니다(스레드로부터 안전).
오버로드
| Name | Description |
|---|---|
| Synchronized(ArrayList) |
ArrayList 동기화된 래퍼를 반환합니다(스레드로부터 안전). |
| Synchronized(IList) |
IList 동기화된 래퍼를 반환합니다(스레드로부터 안전). |
Synchronized(ArrayList)
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 = 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 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안전을 보장하려면 이 래퍼를 통해 모든 작업을 수행해야 합니다.
컬렉션을 열거하는 것은 본질적으로 스레드로부터 안전한 프로시저가 아닙니다. 컬렉션이 동기화된 경우에도 다른 스레드는 컬렉션을 수정할 수 있으므로 열거자가 예외를 throw합니다. 열거 중 스레드 안전을 보장하기 위해 전체 열거 중에 컬렉션을 잠그거나 다른 스레드의 변경으로 인한 예외를 catch할 수 있습니다.
추가 정보
적용 대상
Synchronized(IList)
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 = 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안전을 보장하려면 이 래퍼를 통해 모든 작업을 수행해야 합니다.
컬렉션을 열거하는 것은 본질적으로 스레드로부터 안전한 프로시저가 아닙니다. 컬렉션이 동기화된 경우에도 다른 스레드는 컬렉션을 수정할 수 있으므로 열거자가 예외를 throw합니다. 열거 중 스레드 안전을 보장하기 위해 전체 열거 중에 컬렉션을 잠그거나 다른 스레드의 변경으로 인한 예외를 catch할 수 있습니다.