BlockingCollection - 让消费者等待
BlockingCollection - making consumer wait
使用 Microsoft Docs 中的第二个示例,当我有一个非阻塞消费者时,当 BlockingCollection
中没有项目时让消费者等待的首选方法是什么?文档中的示例如下。
static void NonBlockingConsumer(BlockingCollection<int> bc, CancellationToken ct)
{
// IsCompleted == (IsAddingCompleted && Count == 0)
while (!bc.IsCompleted)
{
int nextItem = 0;
try
{
if (!bc.TryTake(out nextItem, 0, ct))
{
Console.WriteLine(" Take Blocked");
}
else
Console.WriteLine(" Take:{0}", nextItem);
}
catch (OperationCanceledException)
{
Console.WriteLine("Taking canceled.");
break;
}
// Slow down consumer just a little to cause
// collection to fill up faster, and lead to "AddBlocked"
Thread.SpinWait(500000);
}
Console.WriteLine("\r\nNo more items to take.");
}
上面的例子使用了SpinWait
来暂停消费者。
仅使用以下内容可能会使 CPU 非常忙碌。
if (!bc.TryTake(out var item))
{
continue;
}
让消费者等待的首选方法是什么?我计划使用多个 BlockingCollection
并寻找最佳的使用方式。
我建议使用 Take
而不是 TryTake
。
A call to Take may block until an item is available to be removed.
您在问题中提到的 link 有一个很好的(阻塞)示例:
while (!dataItems.IsCompleted)
{
Data data = null;
// Blocks if number.Count == 0
// IOE means that Take() was called on a completed collection.
// Some other thread can call CompleteAdding after we pass the
// IsCompleted check but before we call Take.
// In this example, we can simply catch the exception since the
// loop will break on the next iteration.
try
{
data = dataItems.Take();
}
catch (InvalidOperationException) { }
if (data != null)
{
Process(data);
}
}
Console.WriteLine("\r\nNo more items to take.");
使用 Microsoft Docs 中的第二个示例,当我有一个非阻塞消费者时,当 BlockingCollection
中没有项目时让消费者等待的首选方法是什么?文档中的示例如下。
static void NonBlockingConsumer(BlockingCollection<int> bc, CancellationToken ct)
{
// IsCompleted == (IsAddingCompleted && Count == 0)
while (!bc.IsCompleted)
{
int nextItem = 0;
try
{
if (!bc.TryTake(out nextItem, 0, ct))
{
Console.WriteLine(" Take Blocked");
}
else
Console.WriteLine(" Take:{0}", nextItem);
}
catch (OperationCanceledException)
{
Console.WriteLine("Taking canceled.");
break;
}
// Slow down consumer just a little to cause
// collection to fill up faster, and lead to "AddBlocked"
Thread.SpinWait(500000);
}
Console.WriteLine("\r\nNo more items to take.");
}
上面的例子使用了SpinWait
来暂停消费者。
仅使用以下内容可能会使 CPU 非常忙碌。
if (!bc.TryTake(out var item))
{
continue;
}
让消费者等待的首选方法是什么?我计划使用多个 BlockingCollection
并寻找最佳的使用方式。
我建议使用 Take
而不是 TryTake
。
A call to Take may block until an item is available to be removed.
您在问题中提到的 link 有一个很好的(阻塞)示例:
while (!dataItems.IsCompleted)
{
Data data = null;
// Blocks if number.Count == 0
// IOE means that Take() was called on a completed collection.
// Some other thread can call CompleteAdding after we pass the
// IsCompleted check but before we call Take.
// In this example, we can simply catch the exception since the
// loop will break on the next iteration.
try
{
data = dataItems.Take();
}
catch (InvalidOperationException) { }
if (data != null)
{
Process(data);
}
}
Console.WriteLine("\r\nNo more items to take.");