什么时候BlockingCollection <T> TryTake()可以返回false?

Gyr*_*ien 5 c# collections concurrency

假设BlockingCollection正在使用下面的ConcurrentQueue,如果你使用Timeout.Infinite ,TryTake(T,Int32)方法何时会返回false?

Sve*_*sen 11

这是一个简单的示例,显示何时可以返回false:当集合被标记为CompleteAdding并且变为空时

//by default, BlockingCollection will use ConcurrentQueue
BlockingCollection<int> coll = new BlockingCollection<int>();

coll.Add(1);
coll.Add(2);
coll.CompleteAdding();

int item;

if (coll.TryTake(out item, -1))
{
    Console.WriteLine(item);
}

if (coll.TryTake(out item, -1))
{
    Console.WriteLine(item);
}

if (coll.TryTake(out item, -1))
{
    //this won't get hit
}
else
{
    Console.WriteLine("TryTake returned false!");
}
Run Code Online (Sandbox Code Playgroud)

这允许您禁止在队列中添加新项目并完成剩余元素的处理


Hen*_*man 6

这将打印false:

 var coll = new BlockingCollection<int>();            

 coll.CompleteAdding();   // closed for business

 int v;
 bool result = coll.TryTake(out v, Timeout.Infinite);

 Console.WriteLine(result);
Run Code Online (Sandbox Code Playgroud)

所以基本上BlockingCollection支持2个独立的概念:Empty和Closed.虽然TryTake()可以在Empty队列上永远等待,但当队列为Empty Closed时它将返回false.