ConcurrentQueue<T> ConcurrentQueue<T> ConcurrentQueue<T> ConcurrentQueue<T> Class

定义

表示线程安全的先进先出 (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>ConcurrentQueue<T>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> 实现IReadOnlyCollection<T>开头的接口.NET Framework 4.6.NET Framework 4.6; 在以前版本的.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>() ConcurrentQueue<T>() ConcurrentQueue<T>()

初始化 ConcurrentQueue<T> 类的新实例。Initializes a new instance of the ConcurrentQueue<T> class.

ConcurrentQueue<T>(IEnumerable<T>) ConcurrentQueue<T>(IEnumerable<T>) ConcurrentQueue<T>(IEnumerable<T>) ConcurrentQueue<T>(IEnumerable<T>)

初始化 ConcurrentQueue<T> 类的新实例,该类包含从指定集合中复制的元素Initializes a new instance of the ConcurrentQueue<T> class that contains elements copied from the specified collection

属性

Count Count Count Count

获取 ConcurrentQueue<T> 中包含的元素数。Gets the number of elements contained in the ConcurrentQueue<T>.

IsEmpty IsEmpty IsEmpty IsEmpty

获取一个指示 ConcurrentQueue<T> 是否为空的值。Gets a value that indicates whether the ConcurrentQueue<T> is empty.

方法

Clear() Clear() Clear() Clear()
CopyTo(T[], Int32) CopyTo(T[], Int32) CopyTo(T[], Int32) CopyTo(T[], Int32)

从指定数组索引开始将 ConcurrentQueue<T> 元素复制到现有一维 Array 中。Copies the ConcurrentQueue<T> elements to an existing one-dimensional Array, starting at the specified array index.

Enqueue(T) Enqueue(T) Enqueue(T) Enqueue(T)

将对象添加到 ConcurrentQueue<T> 的结尾处。Adds an object to the end of the ConcurrentQueue<T>.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetEnumerator() GetEnumerator() GetEnumerator() GetEnumerator()

返回循环访问 ConcurrentQueue<T> 的枚举数。Returns an enumerator that iterates through the ConcurrentQueue<T>.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ToArray() ToArray() ToArray() ToArray()

ConcurrentQueue<T> 中存储的元素复制到新数组中。Copies the elements stored in the ConcurrentQueue<T> to a new array.

ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)
TryDequeue(T) TryDequeue(T) TryDequeue(T) TryDequeue(T)

尝试移除并返回位于并发队列开头处的对象。Tries to remove and return the object at the beginning of the concurrent queue.

TryPeek(T) TryPeek(T) TryPeek(T) TryPeek(T)

尝试返回 ConcurrentQueue<T> 开头处的对象但不将其移除。Tries to return an object from the beginning of the ConcurrentQueue<T> without removing it.

显式界面实现

ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32)

从特定的 ICollection 索引开始,将 Array 的元素复制到一个 Array 中。Copies the elements of the ICollection to an Array, starting at a particular Array index.

ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized

获取一个值,该值指示对 ICollection 的访问是否与 SyncRoot 保持同步。Gets a value indicating whether access to the ICollection is synchronized with the SyncRoot.

ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot

获取可用于同步对 ICollection 的访问的对象。Gets an object that can be used to synchronize access to the ICollection. 不支持此属性。This property is not supported.

IEnumerable.GetEnumerator() IEnumerable.GetEnumerator() IEnumerable.GetEnumerator() IEnumerable.GetEnumerator()

返回循环访问集合的枚举数。Returns an enumerator that iterates through a collection.

IProducerConsumerCollection<T>.TryAdd(T) IProducerConsumerCollection<T>.TryAdd(T) IProducerConsumerCollection<T>.TryAdd(T) IProducerConsumerCollection<T>.TryAdd(T)

尝试将一个对象添加到 IProducerConsumerCollection<T> 中。Attempts to add an object to the IProducerConsumerCollection<T>.

IProducerConsumerCollection<T>.TryTake(T) IProducerConsumerCollection<T>.TryTake(T) IProducerConsumerCollection<T>.TryTake(T) IProducerConsumerCollection<T>.TryTake(T)

尝试从 IProducerConsumerCollection<T> 中移除并返回一个对象。Attempts to remove and return an object from the IProducerConsumerCollection<T>.

扩展方法

CopyToDataTable<T>(IEnumerable<T>) CopyToDataTable<T>(IEnumerable<T>) CopyToDataTable<T>(IEnumerable<T>) CopyToDataTable<T>(IEnumerable<T>)

返回DataTable,其中包含的副本DataRow对象,给定输入IEnumerable<T>对象的泛型参数TDataRowReturns 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) CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption) CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption) CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption)

副本DataRow到指定的对象DataTable,给定输入IEnumerable<T>对象的泛型参数TDataRowCopies 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) CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption, FillErrorEventHandler) CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption, FillErrorEventHandler) CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption, FillErrorEventHandler)

副本DataRow到指定的对象DataTable,给定输入IEnumerable<T>对象的泛型参数TDataRowCopies DataRow objects to the specified DataTable, given an input IEnumerable<T> object where the generic parameter T is DataRow.

Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定的类型。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable)

启用查询的并行化。Enables parallelization of a query.

AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable)

将转换IEnumerableIQueryableConverts an IEnumerable to an IQueryable.

Ancestors<T>(IEnumerable<T>) Ancestors<T>(IEnumerable<T>) Ancestors<T>(IEnumerable<T>) 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) Ancestors<T>(IEnumerable<T>, XName) Ancestors<T>(IEnumerable<T>, XName) 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>) DescendantNodes<T>(IEnumerable<T>) DescendantNodes<T>(IEnumerable<T>) DescendantNodes<T>(IEnumerable<T>)

返回源集合中每个文档和元素的子代节点的集合。Returns a collection of the descendant nodes of every document and element in the source collection.

Descendants<T>(IEnumerable<T>) Descendants<T>(IEnumerable<T>) Descendants<T>(IEnumerable<T>) 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) Descendants<T>(IEnumerable<T>, XName) Descendants<T>(IEnumerable<T>, XName) 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>) Elements<T>(IEnumerable<T>) Elements<T>(IEnumerable<T>) 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) Elements<T>(IEnumerable<T>, XName) Elements<T>(IEnumerable<T>, XName) 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>) InDocumentOrder<T>(IEnumerable<T>) InDocumentOrder<T>(IEnumerable<T>) 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>) Nodes<T>(IEnumerable<T>) Nodes<T>(IEnumerable<T>) Nodes<T>(IEnumerable<T>)

返回源集合中每个文档和元素的子节点集合。Returns a collection of the child nodes of every document and element in the source collection.

Remove<T>(IEnumerable<T>) Remove<T>(IEnumerable<T>) Remove<T>(IEnumerable<T>) Remove<T>(IEnumerable<T>)

将源集合中的每个节点从其父节点中移除。Removes every node in the source collection from its parent node.

适用于

线程安全性

所有公共和受保护成员ConcurrentQueue<T>是线程安全的可能从多个线程同时使用。All public and protected members of ConcurrentQueue<T> are thread-safe and may be used concurrently from multiple threads.