c# - Take/TryTake 和 Add/TryAdd 在阻塞集合上的区别

标签 c# .net multithreading asynchronous blockingcollection

我一直在努力思考 Blocking Collection我遇到了 Take()TryTake() 还有 Add()TryAdd()

我知道如果没有要拿的元素,Take() 将等到元素被添加,如果集合已达到它的 Add() 类似最大限制,它将等到项目被删除。

根据 Josheph Albahari's article on Parallel Programming

"Add and TryAdd may block if the collection size is bounded; Take and TryTake block while the collection is empty."

因此 Take()TryTake() 都等待添加一个项目。所以,如果我们不提供任何超时或取消 token ,Take()TryTake() 之间有什么区别,TryTake() return false 直接而不是等待?和 TryAdd() 一样吗?

最佳答案

TryTake不等待,如果集合什么都没有,它立即返回falseTake将等待一个项目。

尝试:

If the collection is empty, this method immediately returns false.

采取:

A call to Take may block until an item is available to be removed.

关于c# - Take/TryTake 和 Add/TryAdd 在阻塞集合上的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48301027/

相关文章:

c# - 解释 expression.Compile()() 中的 2 对括号

ios - NSOperation 内部的异步操作

android - 通过提供一个扩展 Thread 并覆盖其 run() 方法的新类来实现 Thread

c++ - boost 互斥顺序

c# - 通过 SetupApi 找出为设备加载的驱动程序

c# - 将 asp.net 面板呈现的标签从 div 更改为 span

c# - 所有好友列表 C# Facebook SDK v5

c# - C# 中的 Dictionary.Item

c# - 在 asp.net MVC Controller 中调用异步外部 Web 服务

c# - 最小起订量返回未被调用