Stack.Synchronized(Stack) Metode
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Mengembalikan pembungkus yang disinkronkan (aman utas) untuk Stack.
public:
static System::Collections::Stack ^ Synchronized(System::Collections::Stack ^ stack);
public static System.Collections.Stack Synchronized (System.Collections.Stack stack);
static member Synchronized : System.Collections.Stack -> System.Collections.Stack
Public Shared Function Synchronized (stack As Stack) As Stack
Parameter
Mengembalikan
Pembungkus yang disinkronkan di Stacksekitar .
Pengecualian
stack
adalah null
.
Contoh
Contoh berikut menunjukkan cara menyinkronkan Stack, menentukan apakah Stack disinkronkan, dan menggunakan yang disinkronkan Stack.
#using <system.dll>
using namespace System;
using namespace System::Collections;
int main()
{
// Creates and initializes a new Stack.
Stack^ myStack = gcnew Stack;
myStack->Push( "The" );
myStack->Push( "quick" );
myStack->Push( "brown" );
myStack->Push( "fox" );
// Creates a synchronized wrapper around the Stack.
Stack^ mySyncdStack = Stack::Synchronized( myStack );
// Displays the sychronization status of both Stacks.
Console::WriteLine( "myStack is {0}.", myStack->IsSynchronized ? (String^)"synchronized" : "not synchronized" );
Console::WriteLine( "mySyncdStack is {0}.", mySyncdStack->IsSynchronized ? (String^)"synchronized" : "not synchronized" );
}
/*
This code produces the following output.
myStack is not synchronized.
mySyncdStack is synchronized.
*/
using System;
using System.Collections;
public class SamplesStack
{
public static void Main()
{
// Creates and initializes a new Stack.
Stack myStack = new Stack();
myStack.Push("The");
myStack.Push("quick");
myStack.Push("brown");
myStack.Push("fox");
// Creates a synchronized wrapper around the Stack.
Stack mySyncdStack = Stack.Synchronized(myStack);
// Displays the sychronization status of both Stacks.
Console.WriteLine("myStack is {0}.",
myStack.IsSynchronized ? "synchronized" : "not synchronized");
Console.WriteLine("mySyncdStack is {0}.",
mySyncdStack.IsSynchronized ? "synchronized" : "not synchronized");
}
}
/*
This code produces the following output.
myStack is not synchronized.
mySyncdStack is synchronized.
*/
Imports System.Collections
Public Class SamplesStack
Public Shared Sub Main()
' Creates and initializes a new Stack.
Dim myStack As New Stack()
myStack.Push("The")
myStack.Push("quick")
myStack.Push("brown")
myStack.Push("fox")
' Creates a synchronized wrapper around the Stack.
Dim mySyncdStack As Stack = Stack.Synchronized(myStack)
' Displays the sychronization status of both Stacks.
Dim msg As String
If myStack.IsSynchronized Then
msg = "synchronized"
Else
msg = "not synchronized"
End If
Console.WriteLine("myStack is {0}.", msg)
If mySyncdStack.IsSynchronized Then
msg = "synchronized"
Else
msg = "not synchronized"
End If
Console.WriteLine("mySyncdStack is {0}.", msg)
End Sub
End Class
' This code produces the following output.
'
' myStack is not synchronized.
' mySyncdStack is synchronized.
Keterangan
Untuk menjamin keamanan Stackutas , semua operasi harus dilakukan melalui pembungkus ini.
Menghitung melalui koleksi secara intrinsik bukan prosedur aman utas. Bahkan ketika koleksi disinkronkan, utas lain masih dapat memodifikasi koleksi, yang menyebabkan enumerator melemparkan pengecualian. Untuk menjamin keamanan utas selama enumerasi, Anda dapat mengunci koleksi selama seluruh enumerasi atau menangkap pengecualian yang dihasilkan dari perubahan yang dibuat oleh utas lain.
Contoh kode berikut menunjukkan cara mengunci koleksi menggunakan SyncRoot selama seluruh enumerasi.
Stack^ myCollection = gcnew Stack();
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);
}
}
Stack myCollection = new Stack();
lock (myCollection.SyncRoot)
{
foreach (object item in myCollection)
{
// Insert your code here.
}
}
Dim myCollection As New Stack()
SyncLock myCollection.SyncRoot
For Each item As Object In myCollection
' Insert your code here.
Next item
End SyncLock
Metode ini adalah O(1)
operasi.