ConcurrentQueue<T> 類別

定義

表示安全執行緒的先進先出 (FIFO) 集合。Represents a thread-safe first in-first out (FIFO) collection.

generic <typename T>
public ref class ConcurrentQueue : System::Collections::Concurrent::IProducerConsumerCollection<T>, System::Collections::Generic::IEnumerable<T>, System::Collections::Generic::IReadOnlyCollection<T>
[System.Runtime.InteropServices.ComVisible(false)]
[System.Serializable]
public class ConcurrentQueue<T> : System.Collections.Concurrent.IProducerConsumerCollection<T>, System.Collections.Generic.IEnumerable<T>, System.Collections.Generic.IReadOnlyCollection<T>
type ConcurrentQueue<'T> = class
    interface IProducerConsumerCollection<'T>
    interface IReadOnlyCollection<'T>
    interface seq<'T>
    interface ICollection
    interface IEnumerable
Public Class ConcurrentQueue(Of T)
Implements IEnumerable(Of T), IProducerConsumerCollection(Of T), IReadOnlyCollection(Of T)

類型參數

T

佇列中所含項目的類型。The type of the elements contained in the queue.

繼承
ConcurrentQueue<T>
屬性
實作

範例

下列範例顯示如何使用 ConcurrentQueue<T> 來排入佇列和清除佇列專案:The following example shows how to use a ConcurrentQueue<T> to enqueue and dequeue items:

using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;

class CQ_EnqueueDequeuePeek
{
   // Demonstrates:
   // ConcurrentQueue<T>.Enqueue()
   // ConcurrentQueue<T>.TryPeek()
   // ConcurrentQueue<T>.TryDequeue()
   static void Main ()
   {
      // Construct a ConcurrentQueue.
      ConcurrentQueue<int> cq = new ConcurrentQueue<int>();

      // Populate the queue.
      for (int i = 0; i < 10000; i++)
      {
          cq.Enqueue(i);
      }

      // Peek at the first element.
      int result;
      if (!cq.TryPeek(out result))
      {
         Console.WriteLine("CQ: TryPeek failed when it should have succeeded");
      }
      else if (result != 0)
      {
         Console.WriteLine("CQ: Expected TryPeek result of 0, got {0}", result);
      }

      int outerSum = 0;
      // An action to consume the ConcurrentQueue.
      Action action = () =>
      {                
         int localSum = 0;
         int localValue;
         while (cq.TryDequeue(out localValue)) localSum += localValue;
         Interlocked.Add(ref outerSum, localSum);
      };

      // Start 4 concurrent consuming actions.
      Parallel.Invoke(action, action, action, action);

      Console.WriteLine("outerSum = {0}, should be 49995000", outerSum);
   }
}
Imports System.Collections.Concurrent
Imports System.Threading
Imports System.Threading.Tasks

Class TestQueue
   ' Demonstrates:
   ' ConcurrentQueue<T>.Enqueue()
   ' ConcurrentQueue<T>.TryPeek()
   ' ConcurrentQueue<T>.TryDequeue()
   Shared Sub Main()
      ' Construct a ConcurrentQueue
      Dim cq As New ConcurrentQueue(Of Integer)()

      ' Populate the queue
      For i As Integer = 0 To 9999
         cq.Enqueue(i)
      Next

      ' Peek at the first element
      Dim result As Integer
      If Not cq.TryPeek(result) Then
         Console.WriteLine("CQ: TryPeek failed when it should have succeeded")
      ElseIf result <> 0 Then
         Console.WriteLine("CQ: Expected TryPeek result of 0, got {0}", result)
      End If

      Dim outerSum As Integer = 0

      ' An action to consume the ConcurrentQueue
      Dim action As Action =
          Sub()
             Dim localSum As Integer = 0
             Dim localValue As Integer
             While cq.TryDequeue(localValue)
                localSum += localValue
             End While
             Interlocked.Add(outerSum, localSum)
          End Sub

      ' Start 4 concurrent consuming actions
      Parallel.Invoke(action, action, action, action)

      Console.WriteLine("outerSum = {0}, should be 49995000", outerSum)
   End Sub
End Class

備註

注意

ConcurrentQueue<T> 會執行從 .NET Framework 4.6.NET Framework 4.6 開始的 @no__t 1 介面;在舊版的 .NET Framework 中,ConcurrentQueue<T> 類別並未執行此介面。ConcurrentQueue<T> implements the IReadOnlyCollection<T> interface starting with the .NET Framework 4.6.NET Framework 4.6; in previous versions of the .NET Framework, the ConcurrentQueue<T> class did not implement this interface.

建構函式

ConcurrentQueue<T>()

初始化 ConcurrentQueue<T> 類別的新執行個體。Initializes a new instance of the ConcurrentQueue<T> class.

ConcurrentQueue<T>(IEnumerable<T>)

初始化 ConcurrentQueue<T> 類別的新執行個體,這個類別包含自指定之集合複製過來的項目。Initializes a new instance of the ConcurrentQueue<T> class that contains elements copied from the specified collection.

屬性

Count

取得 ConcurrentQueue<T> 中所包含的項目數。Gets the number of elements contained in the ConcurrentQueue<T>.

IsEmpty

取得值,這個值指出 ConcurrentQueue<T> 是否是空的。Gets a value that indicates whether the ConcurrentQueue<T> is empty.

方法

Clear()

ConcurrentQueue<T> 移除所有物件。Removes all objects from the ConcurrentQueue<T>.

CopyTo(T[], Int32)

從指定的陣列索引處開始,複製 ConcurrentQueue<T> 項目至現有一維 ArrayCopies the ConcurrentQueue<T> elements to an existing one-dimensional Array, starting at the specified array index.

Enqueue(T)

將物件加入至 ConcurrentQueue<T> 的末端。Adds an object to the end of the ConcurrentQueue<T>.

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetEnumerator()

傳回在 ConcurrentQueue<T> 中逐一查看的列舉值。Returns an enumerator that iterates through the ConcurrentQueue<T>.

GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToArray()

將存放在 ConcurrentQueue<T> 中的項目複製到新的陣列。Copies the elements stored in the ConcurrentQueue<T> to a new array.

ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(繼承來源 Object)
TryDequeue(T)

嘗試移除並傳回位在並行佇列前端的物件。Tries to remove and return the object at the beginning of the concurrent queue.

TryPeek(T)

嘗試傳回在 ConcurrentQueue<T> 前端的物件,但不將它移除。Tries to return an object from the beginning of the ConcurrentQueue<T> without removing it.

明確介面實作

ICollection.CopyTo(Array, Int32)

從特定的 ICollection 索引開始,將 Array 的項目複製到 ArrayCopies the elements of the ICollection to an Array, starting at a particular Array index.

ICollection.IsSynchronized

取得值,這個值表示對 ICollection 的存取是否已經與 SyncRoot 同步。Gets a value indicating whether access to the ICollection is synchronized with the SyncRoot.

ICollection.SyncRoot

取得可用以同步存取 ICollection 的物件。Gets an object that can be used to synchronize access to the ICollection. 不支援這個屬性。This property is not supported.

IEnumerable.GetEnumerator()

傳回逐一查看集合的列舉值。Returns an enumerator that iterates through a collection.

IProducerConsumerCollection<T>.TryAdd(T)

嘗試將物件加入至 IProducerConsumerCollection<T>Attempts to add an object to the IProducerConsumerCollection<T>.

IProducerConsumerCollection<T>.TryTake(T)

嘗試從 IProducerConsumerCollection<T> 中移除及傳回物件。Attempts to remove and return an object from the IProducerConsumerCollection<T>.

擴充方法

CopyToDataTable<T>(IEnumerable<T>)

根據輸入 DataTable 物件 (其中泛型參數 TDataRow) 傳回包含 IEnumerable<T> 物件複本的 DataRowReturns a DataTable that contains copies of the DataRow objects, given an input IEnumerable<T> object where the generic parameter T is DataRow.

CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption)

根據輸入 DataRow 物件 (其中泛型參數 TDataTable),將 IEnumerable<T> 物件複製到指定的 DataRowCopies DataRow objects to the specified DataTable, given an input IEnumerable<T> object where the generic parameter T is DataRow.

CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption, FillErrorEventHandler)

根據輸入 DataRow 物件 (其中泛型參數 TDataTable),將 IEnumerable<T> 物件複製到指定的 DataRowCopies DataRow objects to the specified DataTable, given an input IEnumerable<T> object where the generic parameter T is DataRow.

Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable)

啟用查詢的平行化作業。Enables parallelization of a query.

AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryableConverts an IEnumerable to an IQueryable.

Ancestors<T>(IEnumerable<T>)

傳回包含來源集合中每個節點祖系的項目集合。Returns a collection of elements that contains the ancestors of every node in the source collection.

Ancestors<T>(IEnumerable<T>, XName)

傳回包含來源集合中每個節點祖系的已篩選項目集合。Returns a filtered collection of elements that contains the ancestors of every node in the source collection. 集合中只會包含具有相符之 XName 的項目。Only elements that have a matching XName are included in the collection.

DescendantNodes<T>(IEnumerable<T>)

傳回來源集合中每個文件和項目之子代節點的集合。Returns a collection of the descendant nodes of every document and element in the source collection.

Descendants<T>(IEnumerable<T>)

傳回包含來源集合中每個項目和文件之子代項目的項目集合。Returns a collection of elements that contains the descendant elements of every element and document in the source collection.

Descendants<T>(IEnumerable<T>, XName)

傳回已篩選的項目集合,其中包含來源集合中每個項目和文件的子代項目。Returns a filtered collection of elements that contains the descendant elements of every element and document in the source collection. 集合中只會包含具有相符之 XName 的項目。Only elements that have a matching XName are included in the collection.

Elements<T>(IEnumerable<T>)

傳回來源集合中每個項目和文件的子項目集合。Returns a collection of the child elements of every element and document in the source collection.

Elements<T>(IEnumerable<T>, XName)

傳回來源集合中每個項目和文件的已篩選子項目集合。Returns a filtered collection of the child elements of every element and document in the source collection. 集合中只會包含具有相符之 XName 的項目。Only elements that have a matching XName are included in the collection.

InDocumentOrder<T>(IEnumerable<T>)

傳回包含來源集合中所有節點的節點集合,依據文件順序來排序。Returns a collection of nodes that contains all nodes in the source collection, sorted in document order.

Nodes<T>(IEnumerable<T>)

傳回來源集合中每個文件和項目的子節點集合。Returns a collection of the child nodes of every document and element in the source collection.

Remove<T>(IEnumerable<T>)

在來源集合中,從每一個節點的父節點移除這些節點。Removes every node in the source collection from its parent node.

適用於

執行緒安全性

@No__t-0 的所有 public 和 protected 成員都是安全線程,而且可以從多個執行緒同時使用。All public and protected members of ConcurrentQueue<T> are thread-safe and may be used concurrently from multiple threads.