ConcurrentQueue<T> 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示线程安全的先出 (FIFO) 集合。
generic <typename T>
public ref class ConcurrentQueue : System::Collections::Concurrent::IProducerConsumerCollection<T>, System::Collections::Generic::IEnumerable<T>, System::Collections::Generic::IReadOnlyCollection<T>, System::Collections::ICollection
generic <typename T>
public ref class ConcurrentQueue : System::Collections::Concurrent::IProducerConsumerCollection<T>, System::Collections::Generic::IEnumerable<T>
generic <typename T>
public ref class ConcurrentQueue : System::Collections::Concurrent::IProducerConsumerCollection<T>, System::Collections::Generic::IEnumerable<T>, System::Collections::Generic::IReadOnlyCollection<T>
generic <typename T>
public ref class ConcurrentQueue : System::Collections::Concurrent::IProducerConsumerCollection<T>, System::Collections::Generic::IEnumerable<T>, System::Collections::ICollection
public class ConcurrentQueue<T> : System.Collections.Concurrent.IProducerConsumerCollection<T>, System.Collections.Generic.IEnumerable<T>, System.Collections.Generic.IReadOnlyCollection<T>, System.Collections.ICollection
[System.Runtime.InteropServices.ComVisible(false)]
[System.Serializable]
public class ConcurrentQueue<T> : System.Collections.Concurrent.IProducerConsumerCollection<T>, System.Collections.Generic.IEnumerable<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>
public class ConcurrentQueue<T> : System.Collections.Concurrent.IProducerConsumerCollection<T>, System.Collections.Generic.IEnumerable<T>, System.Collections.ICollection
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 seq<'T>
interface IEnumerable
interface ICollection
interface IReadOnlyCollection<'T>
[<System.Runtime.InteropServices.ComVisible(false)>]
[<System.Serializable>]
type ConcurrentQueue<'T> = class
interface IProducerConsumerCollection<'T>
interface seq<'T>
interface ICollection
interface IEnumerable
[<System.Runtime.InteropServices.ComVisible(false)>]
[<System.Serializable>]
type ConcurrentQueue<'T> = class
interface IProducerConsumerCollection<'T>
interface seq<'T>
interface IEnumerable
interface ICollection
interface IReadOnlyCollection<'T>
type ConcurrentQueue<'T> = class
interface IProducerConsumerCollection<'T>
interface seq<'T>
interface ICollection
interface IEnumerable
Public Class ConcurrentQueue(Of T)
Implements ICollection, IEnumerable(Of T), IProducerConsumerCollection(Of T), IReadOnlyCollection(Of T)
Public Class ConcurrentQueue(Of T)
Implements IEnumerable(Of T), IProducerConsumerCollection(Of T)
Public Class ConcurrentQueue(Of T)
Implements IEnumerable(Of T), IProducerConsumerCollection(Of T), IReadOnlyCollection(Of T)
Public Class ConcurrentQueue(Of T)
Implements ICollection, IEnumerable(Of T), IProducerConsumerCollection(Of T)
类型参数
- T
队列中包含的元素的类型。
- 继承
-
ConcurrentQueue<T>
- 属性
- 实现
示例
以下示例演示如何使用 a ConcurrentQueue<T> 来排队和取消排队项:
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);
}
}
open System
open System.Collections.Concurrent
open System.Threading
open System.Threading.Tasks
// Demonstrates:
// ConcurrentQueue<T>.Enqueue()
// ConcurrentQueue<T>.TryPeek()
// ConcurrentQueue<T>.TryDequeue()
// Construct a ConcurrentQueue.
let cq = ConcurrentQueue<int>()
// Populate the queue.
for i = 0 to 9999 do
cq.Enqueue i
// Peek at the first element.
let mutable result = 0
if cq.TryPeek &result |> not then
printfn "CQ: TryPeek failed when it should have succeeded"
elif result <> 0 then
printfn $"CQ: Expected TryPeek result of 0, got {result}"
let mutable outerSum = 0
// An action to consume the ConcurrentQueue.
let action =
Action(fun () ->
let mutable localSum = 0
let mutable localValue = 0
while cq.TryDequeue &localValue do
localSum <- localSum + localValue
Interlocked.Add(&outerSum, localSum) |> ignore)
// Start 4 concurrent consuming actions.
Parallel.Invoke(action, action, action, action)
printfn $"outerSum = {outerSum}, should be 49995000"
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 开始实现 IReadOnlyCollection<T> 接口;在 .NET Framework 的早期版本中,ConcurrentQueue<T> 类未实现此接口。
构造函数
| 名称 | 说明 |
|---|---|
| ConcurrentQueue<T>() |
初始化 ConcurrentQueue<T> 类的新实例。 |
| ConcurrentQueue<T>(IEnumerable<T>) |
初始化包含从指定集合复制的元素的 ConcurrentQueue<T> 类的新实例。 |
属性
| 名称 | 说明 |
|---|---|
| Count |
获取包含在 . 中的 ConcurrentQueue<T>元素数。 |
| IsEmpty |
获取一个值,该值指示是否为 ConcurrentQueue<T> 空。 |
方法
| 名称 | 说明 |
|---|---|
| Clear() |
从 ConcurrentQueue<T>.. 中删除所有对象 |
| CopyTo(T[], Int32) |
从指定的数组索引处开始,将 ConcurrentQueue<T> 元素复制到现有的一维 Array。 |
| Enqueue(T) |
将对象添加到该 ConcurrentQueue<T>对象的末尾。 |
| Equals(Object) |
确定指定的对象是否等于当前对象。 (继承自 Object) |
| GetEnumerator() |
返回循环访问的 ConcurrentQueue<T>枚举数。 |
| GetHashCode() |
用作默认哈希函数。 (继承自 Object) |
| GetType() |
获取当前实例的 Type。 (继承自 Object) |
| MemberwiseClone() |
创建当前 Object的浅表副本。 (继承自 Object) |
| ToArray() |
将存储在该数组中的 ConcurrentQueue<T> 元素复制到新数组。 |
| ToString() |
返回一个表示当前对象的字符串。 (继承自 Object) |
| TryDequeue(T) |
尝试删除并返回并发队列开头的对象。 |
| TryPeek(T) |
尝试从头开始 ConcurrentQueue<T> 返回对象而不将其删除。 |
显式接口实现
| 名称 | 说明 |
|---|---|
| ICollection.CopyTo(Array, Int32) |
将元素ICollection复制到从特定Array索引开始的元素Array。 |
| ICollection.IsSynchronized |
获取一个值,该值指示访问 ICollection 是否与 SyncRoot 同步。 |
| ICollection.SyncRoot |
获取可用于同步对 . ICollection的访问的对象。 不支持此属性。 |
| IEnumerable.GetEnumerator() |
返回循环访问集合的枚举器。 |
| IProducerConsumerCollection<T>.TryAdd(T) |
尝试将对象添加到 .IProducerConsumerCollection<T> |
| IProducerConsumerCollection<T>.TryTake(T) |
尝试从 . IProducerConsumerCollection<T>. 中删除和返回对象。 |
扩展方法
适用于
线程安全性
所有公共成员和受保护的成员 ConcurrentQueue<T> 都是线程安全的,并且可以同时从多个线程使用。