簡體   English   中英

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

調用Take可能會阻塞,直到可以刪除某項為止。

您在問題中提到的鏈接有一個很好的(阻止)示例:

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.");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM