gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 19:36:05 25 4
gpt4 key购买 nike

我一直在努力思考 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com