Queue.IsSynchronized 属性

获取一个值,该值指示是否同步对 Queue 的访问(线程安全)。

**命名空间:**System.Collections
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public Overridable ReadOnly Property IsSynchronized As Boolean
用法
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

属性值

如果对 Queue 的访问是同步的(线程安全),则为 true;否则为 false。默认为 false

备注

若要保证 Queue 的线程安全,则必须通过由 Synchronized 方法返回的包装来执行所有操作。

通过集合枚举在本质上不是一个线程安全的过程。即使一个集合已进行同步,其他线程仍可以修改该集合,这将导致枚举数引发异常。若要在枚举过程中保证线程安全,可以在整个枚举过程中锁定集合,或者捕捉由于其他线程进行的更改而引发的异常。

示例

下面的代码示例演示如何在整个枚举过程中使用 SyncRoot 锁定集合。检索此属性的值的运算复杂度为 O(1)。

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

下面的示例演示如何同步 Queue、如何确定 Queue 是否同步以及如何使用同步的 Queue

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.
 */

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

Queue 类
Queue 成员
System.Collections 命名空间
SyncRoot
Synchronized