我一直在努力思考 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
不等待,如果集合什么都没有,它立即返回false
。 Take
将等待一个项目。
尝试:
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/