.net - ConcurrentQueue<> 中的 TryDequeue 和 TryTake 有什么区别?
问题描述
在ConcurrentQeueue<>
类中,TryDequeue()
定义了额外的方法。但是,当它实现IProducerConsumerCollection<>
时,它也有一个TryTake()
方法。根据文档,他们都做同样的事情:
尝试出队:
尝试在并发队列的开头删除并返回对象。
尝试:
对于 ConcurrentQueue,此操作将尝试从 ConcurrentQueue 的开头移除对象。
为什么要为实现该TryDequeue
方法而烦恼?
解决方案
ConcurrentQueue<> 中的 TryDequeue 和 TryTake 有什么区别
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
为什么还要费心实现那个 TryDequeue 方法呢?
TryDequeue
遵循与队列相关的预期名称约定,并且是ConcurrentQueue<>
. 同样,TryTake
遵循通常与生产者/消费者模式相关的命名约定。
推荐阅读
- php - 我如何将 php 连接到 arduino esp32
- r - 有没有办法修复 R Plotly 中水平条形图的条形高度?
- c++ - 在深 std::unordered_map 中插入唯一指针
- json - 尝试在 Groovy 中创建 JSON 结构时出现令人困惑的错误
- javascript - 如果取决于选择的下拉选项,如何隐藏 div
- google-cloud-platform - 无法使用公司 Google 帐户将 GCP 与 Dialogflow 连接起来
- file-put-contents - file_put_contents 需要限制行数
- flutter - 颤振错误:由于颤振测试无法识别,Widgets.test 出错
- android - 什么确切的调用启动了在屏幕上绘制视图?
- python - Django - 我如何将“.json”文件插入 SQLite DB?