Freigeben über


Queue.IsSynchronized-Eigenschaft

Ruft einen Wert ab, der angibt, ob der Zugriff auf die Queue synchronisiert (threadsicher) ist.

Namespace: System.Collections
Assembly: mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Overridable ReadOnly Property IsSynchronized As Boolean
'Usage
Dim instance As Queue
Dim value As Boolean

value = instance.IsSynchronized
public virtual bool IsSynchronized { get; }
public:
virtual property bool IsSynchronized {
    bool get ();
}
/** @property */
public boolean get_IsSynchronized ()
public function get IsSynchronized () : boolean

Eigenschaftenwert

true, wenn der Zugriff auf die Queue synchronisiert (threadsicher) ist, andernfalls false. Der Standardwert ist false.

Hinweise

Zur Gewährleistung der Threadsicherheit der Queue müssen alle Operationen unter Verwendung des Wrappers ausgeführt werden, der von der Synchronized-Methode zurückgegeben wird.

Die Enumeration einer Auflistung ist systemintern keine threadsichere Prozedur. Selbst wenn eine Auflistung synchronisiert ist, besteht die Möglichkeit, dass andere Threads sie ändern. Dies führt dazu, dass der Enumerator eine Ausnahme auslöst. Sie können während der Enumeration Threadsicherheit gewährleisten, indem Sie entweder die Auflistung während der gesamten Enumeration sperren oder die Ausnahmen abfangen, die durch Änderungen ausgelöst werden, die von anderen Threads vorgenommen werden.

Beispiel

Das folgende Codebeispiel veranschaulicht, wie die Auflistung mithilfe von SyncRoot während der gesamten Enumeration gesperrt wird: Das Abrufen des Werts dieser Eigenschaft ist ein O(1)-Vorgang.

Queue myCollection = new Queue();
  lock(myCollection.SyncRoot) {
  foreach (Object item in myCollection) {
  // Insert your code here.
  }
 }
Dim myCollection As New Queue()
 Dim item As Object
 SyncLock myCollection.SyncRoot
  For Each item In myCollection
  ' Insert your code here.
  Next item
 End SyncLock

Im folgenden Beispiel wird gezeigt, wie Sie einen Queue synchronisieren, einen synchronisierten Queue verwenden und wie Sie ermitteln, ob ein Queue synchronisiert ist.

Imports System
Imports System.Collections

Public Class SamplesQueue    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new Queue.
        Dim myQ As New Queue()
        myQ.Enqueue("The")
        myQ.Enqueue("quick")
        myQ.Enqueue("brown")
        myQ.Enqueue("fox")
        
        ' Creates a synchronized wrapper around the Queue.
        Dim mySyncdQ As Queue = Queue.Synchronized(myQ)
        
        ' Displays the sychronization status of both Queues.
        Dim msg As String
        If myQ.IsSynchronized Then
            msg = "synchronized"
        Else
            msg = "not synchronized"
        End If
        Console.WriteLine("myQ is {0}.", msg)
        If mySyncdQ.IsSynchronized Then
            msg = "synchronized"
        Else
            msg = "not synchronized"
        End If
        Console.WriteLine("mySyncdQ is {0}.", msg)
    End Sub
End Class

' This code produces the following output.
' 
' myQ is not synchronized.
' mySyncdQ is synchronized. 
using System;
using System.Collections;
public class SamplesQueue  {

   public static void Main()  {

      // Creates and initializes a new Queue.
      Queue myQ = new Queue();
      myQ.Enqueue( "The" );
      myQ.Enqueue( "quick" );
      myQ.Enqueue( "brown" );
      myQ.Enqueue( "fox" );

      // Creates a synchronized wrapper around the Queue.
      Queue mySyncdQ = Queue.Synchronized( myQ );

      // Displays the sychronization status of both Queues.
      Console.WriteLine( "myQ is {0}.", myQ.IsSynchronized ? "synchronized" : "not synchronized" );
      Console.WriteLine( "mySyncdQ is {0}.", mySyncdQ.IsSynchronized ? "synchronized" : "not synchronized" );
   }
}
/* 
This code produces the following output.

myQ is not synchronized.
mySyncdQ is synchronized.
*/ 
#using <system.dll>

using namespace System;
using namespace System::Collections;
int main()
{
   
   // Creates and initializes a new Queue.
   Queue^ myQ = gcnew Queue;
   myQ->Enqueue( "The" );
   myQ->Enqueue( "quick" );
   myQ->Enqueue( "brown" );
   myQ->Enqueue( "fox" );
   
   // Creates a synchronized wrapper around the Queue.
   Queue^ mySyncdQ = Queue::Synchronized( myQ );
   
   // Displays the sychronization status of both Queues.
   Console::WriteLine( "myQ is {0}.", myQ->IsSynchronized ? (String^)"synchronized" : "not synchronized" );
   Console::WriteLine( "mySyncdQ is {0}.", mySyncdQ->IsSynchronized ? (String^)"synchronized" : "not synchronized" );
}

/*
This code produces the following output.

myQ is not synchronized.
mySyncdQ is synchronized.
*/
import System.*;
import System.Collections.*;

public class SamplesQueue
{
    public static void main(String[] args)
    {
        // Creates and initializes a new Queue.
        Queue myQ =  new Queue();
        myQ.Enqueue("The");
        myQ.Enqueue("quick");
        myQ.Enqueue("brown");
        myQ.Enqueue("fox");

        // Creates a synchronized wrapper around the Queue.
        Queue mySyncdQ = Queue.Synchronized(myQ);

        // Displays the sychronization status of both Queues.
        Console.WriteLine("myQ is {0}.",
            (myQ.get_IsSynchronized()) ? "synchronized" : "not synchronized");
        
        Console.WriteLine("mySyncdQ is {0}.",(mySyncdQ.get_IsSynchronized()) ?
            "synchronized" : "not synchronized");
    } //main
} //SamplesQueue

/* 
 This code produces the following output.
 
 myQ is not synchronized.
 mySyncdQ is synchronized.
 */

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

Queue-Klasse
Queue-Member
System.Collections-Namespace
SyncRoot
Synchronized