ConcurrentQueue<> 中的 TryDequeue 和 TryTake 有什么区别?
What is the difference between TryDequeue and TryTake in a ConcurrentQueue<>?
在ConcurrentQeueue<>
class中定义了额外的方法TryDequeue()
。但是,当它实现 IProducerConsumerCollection<>
时,它也有一个 TryTake()
方法。根据文档,他们都做同样的事情:
Tries to remove and return the object at the beginning of the concurrent queue.
For ConcurrentQueue, this operation will attempt to remove the object from the beginning of the ConcurrentQueue.
为什么要费心实施 TryDequeue
方法?
What is the difference between TryDequeue and TryTake in a ConcurrentQueue<>
根据可用的源代码,TryTake
调用 TryDequeue
没有区别
/// <summary>
/// Attempts to remove and return an object from the <see cref="Concurrent.IProducerConsumerCollection{T}"/>.
/// </summary>
/// <param name="item">
/// When this method returns, if the operation was successful, <paramref name="item"/> contains the
/// object removed. If no object was available to be removed, the value is unspecified.
/// </param>
/// <returns>true if an element was removed and returned successfully; otherwise, false.</returns>
/// <remarks>For <see cref="ConcurrentQueue{T}"/>, this operation will attempt to remove the object
/// from the beginning of the <see cref="ConcurrentQueue{T}"/>.
/// </remarks>
bool IProducerConsumerCollection<T>.TryTake([MaybeNullWhen(false)] out T item) => TryDequeue(out item);
来源:https://source.dot.net/#System.Private.CoreLib/ConcurrentQueue.cs,201
Why bother with implementing that TryDequeue method?
TryDequeue
遵循与队列关联的预期名称约定,并且是 ConcurrentQueue<>
的本地名称。同样,TryTake
遵循通常与 producer/consumer 模式关联的命名约定。
在ConcurrentQeueue<>
class中定义了额外的方法TryDequeue()
。但是,当它实现 IProducerConsumerCollection<>
时,它也有一个 TryTake()
方法。根据文档,他们都做同样的事情:
Tries to remove and return the object at the beginning of the concurrent queue.
For ConcurrentQueue, this operation will attempt to remove the object from the beginning of the ConcurrentQueue.
为什么要费心实施 TryDequeue
方法?
What is the difference between TryDequeue and TryTake in a ConcurrentQueue<>
根据可用的源代码,TryTake
调用 TryDequeue
/// <summary>
/// Attempts to remove and return an object from the <see cref="Concurrent.IProducerConsumerCollection{T}"/>.
/// </summary>
/// <param name="item">
/// When this method returns, if the operation was successful, <paramref name="item"/> contains the
/// object removed. If no object was available to be removed, the value is unspecified.
/// </param>
/// <returns>true if an element was removed and returned successfully; otherwise, false.</returns>
/// <remarks>For <see cref="ConcurrentQueue{T}"/>, this operation will attempt to remove the object
/// from the beginning of the <see cref="ConcurrentQueue{T}"/>.
/// </remarks>
bool IProducerConsumerCollection<T>.TryTake([MaybeNullWhen(false)] out T item) => TryDequeue(out item);
来源:https://source.dot.net/#System.Private.CoreLib/ConcurrentQueue.cs,201
Why bother with implementing that TryDequeue method?
TryDequeue
遵循与队列关联的预期名称约定,并且是 ConcurrentQueue<>
的本地名称。同样,TryTake
遵循通常与 producer/consumer 模式关联的命名约定。