如何:在 BlockingCollection 中逐个添加和取出项

本示例演示如何按照阻塞方式和非阻塞方式在 BlockingCollection<T> 中添加和移除项。 有关 BlockingCollection<T> 的更多信息,请参见 BlockingCollection 概述

有关如何枚举 BlockingCollection<T> 直至其变为空且不再添加元素的示例,请参见如何:使用 ForEach 移除 BlockingCollection 中的项

示例

此第一个示例演示如何添加和取出项,以便相关操作在以下情况下发生阻塞:集合临时为空(在取出项时)或达到最大容量(在添加项时),或者已超过指定的超时时间。 请注意,只有在创建 BlockingCollection 时在构造函数中指定了最大容量的情况下,才支持在达到最大容量时进行阻塞。

Option Strict On
Option Explicit On

Imports System.Collections.Concurrent
Imports System.Threading
Imports System.Threading.Tasks

Module SimpleBlocking

    Class Program
        Shared Sub Main()
            ' Increase or decrease this value as desired.
            Dim itemsToAdd As Integer = 500

            ' Preserve all the display output for Adds and Takes
            Console.SetBufferSize(80, (itemsToAdd * 2) + 3)

            ' A bounded collection. Increase, decrease, or remove the 
            ' maximum capacity argument to see how it impacts behavior.
            Dim numbers = New BlockingCollection(Of Integer)(50)

            ' A simple blocking consumer with no cancellation.
            Task.Factory.StartNew(Sub()
                                      Dim i As Integer = -1
                                      While numbers.IsCompleted = False
                                          Try
                                              i = numbers.Take()
                                          Catch ioe As InvalidOperationException
                                              Console.WriteLine("Adding was completed!")
                                              Exit While
                                          End Try
                                          Console.WriteLine("Take:{0} ", i)
                                          ' Simulate a slow consumer. This will cause
                                          ' collection to fill up fast and thus Adds wil block.
                                          Thread.SpinWait(100000)
                                      End While
                                      Console.WriteLine(vbCrLf & "No more items to take. Press the Enter key to exit.")
                                  End Sub)

            ' A simple blocking producer with no cancellation.
            Task.Factory.StartNew(Sub()
                                      For i As Integer = 0 To itemsToAdd
                                          numbers.Add(i)
                                          Console.WriteLine("Add:{0} Count={1}", i, numbers.Count)
                                      Next

                                      'See documentation for this method.
                                      numbers.CompleteAdding()
                                  End Sub)

            'Keep the console window open in debug mode.
            Console.ReadLine()
        End Sub
    End Class

End Module
namespace BCBlockingAccess
{
    using System;
    using System.Collections.Concurrent;


    class Program
    {
        static void Main(string[] args)
        {
            // Increase or decrease this value as desired.
            int itemsToAdd = 500;

            // Preserve all the display output for Adds and Takes
            Console.SetBufferSize(80, (itemsToAdd * 2) + 3);

            // A bounded collection. Increase, decrease, or remove the 
            // maximum capacity argument to see how it impacts behavior.
            BlockingCollection<int> numbers = new BlockingCollection<int>(50);


            // A simple blocking consumer with no cancellation.
            Task.Factory.StartNew(() =>
            {
                int i = -1;
                while (!numbers.IsCompleted)
                {
                    try
                    {
                        i = numbers.Take();
                    }
                    catch (InvalidOperationException)
                    {
                        Console.WriteLine("Adding was compeleted!");
                        break;
                    }
                    Console.WriteLine("Take:{0} ", i);

                    // Simulate a slow consumer. This will cause
                    // collection to fill up fast and thus Adds wil block.
                    Thread.SpinWait(100000);
                }

                Console.WriteLine("\r\nNo more items to take. Press the Enter key to exit.");
            });

            // A simple blocking producer with no cancellation.
            Task.Factory.StartNew(() =>
            {
                for (int i = 0; i < itemsToAdd; i++)
                {
                    numbers.Add(i);
                    Console.WriteLine("Add:{0} Count={1}", i, numbers.Count);
                }

                // See documentation for this method.
                numbers.CompleteAdding();
            });

            // Keep the console display open in debug mode.

            Console.ReadLine();
        }
    }
}

此第二个示例演示如何添加和取出项,以便相关操作不会发生阻塞。 如果集合中不存在任何项、已达到有限集合的最大容量或超时时间已到,则 TryAdd() 或 TryTake() 操作将返回 false。 这将允许线程执行一段时间的其他有用工作,过一会再重新尝试检索新项或尝试添加之前未能添加的相同项。 该程序还演示如何在访问 BlockingCollection<T> 时实现取消。

Option Strict On
Option Explicit On
Imports System.Collections.Concurrent
Imports System.Threading
Imports System.Threading.Tasks
Module NonBlockingBC


    Class NonBlockingAccess
        Shared inputs As Integer
        Shared Sub Main()
            ' The token source for issuing the cancelation request.
            Dim cts As CancellationTokenSource = New CancellationTokenSource()

            ' A blocking collection that can hold no more than 100 items at a time.
            Dim numberCollection As BlockingCollection(Of Integer) = New BlockingCollection(Of Integer)(100)

            ' Set console buffer to hold our prodigious output.
            Console.SetBufferSize(80, 2000)

            ' The simplest UI thread ever invented.
            Task.Factory.StartNew(Sub()
                                      If Console.ReadKey.KeyChar() = "c"c Then
                                          cts.Cancel()
                                      End If
                                  End Sub)
            ' Start one producer and one consumer.
            Task.Factory.StartNew(Sub() NonBlockingConsumer(numberCollection, cts.Token))
            Task.Factory.StartNew(Sub() NonBlockingProducer(numberCollection, cts.Token))


            Console.WriteLine("Press the Enter key to exit.")
            Console.ReadLine()
        End Sub

        Shared Sub NonBlockingConsumer(ByVal bc As BlockingCollection(Of Integer), ByVal ct As CancellationToken)

            ' IsCompleted is equivalent to (IsAddingCompleted And Count = 0)
            While bc.IsCompleted = False
                Dim nextItem As Integer = 0
                Try
                    If bc.TryTake(nextItem, 0, ct) Then
                        Console.WriteLine("  Take Blocked.")
                    Else
                        Console.WriteLine(" Take: {0}", nextItem)
                    End If
                Catch ex As OperationCanceledException
                    Console.WriteLine("Taking canceled.")
                    Exit While
                End Try
                'Slow down consumer just a little to cause
                ' collection to fill up faster, and lead to "AddBlocked"
                Thread.SpinWait(500000)
            End While

            Console.WriteLine(vbCrLf & "No more items to take. Press the Enter key to exit.")
        End Sub

        Shared Sub NonBlockingProducer(ByVal bc As BlockingCollection(Of Integer), ByVal ct As CancellationToken)
            Dim itemToAdd As Integer = 0
            Dim success As Boolean = False

            Do While itemToAdd < inputs
                'Cancellation causes OCE. We know how to handle it.
                Try
                    success = bc.TryAdd(itemToAdd, 2, ct)
                Catch ex As OperationCanceledException
                    Console.WriteLine("Add loop canceled.")

                    ' Let other threads know we're done in case
                    ' they aren't monitoring the cancellation token.
                    bc.CompleteAdding()
                    Exit Do
                End Try

                If success = True Then
                    Console.WriteLine(" Add:{0}", itemToAdd)
                    itemToAdd = itemToAdd + 1
                Else
                    Console.Write("  AddBlocked:{0} Count = {1}", itemToAdd.ToString(), bc.Count)

                    ' Don't increment nextItem. Try again on next iteration
                    ' Do something else useful instead.
                    UpdateProgress(itemToAdd)
                End If
            Loop
        End Sub

        Shared Sub UpdateProgress(ByVal i As Integer)
            Dim percent As Double = (CType(i, Double) / inputs) * 100
            Console.WriteLine("Percent complete: {0}", percent)
        End Sub
    End Class

End Module
namespace BCNonBlockingWithCancellation
{
    using System;
    using System.Collections.Concurrent;

    class ProgramWithCancellation
    {

        static int inputs = 2000;
        static void Main(string[] args)
        {
            // The token source for issuing the cancelation request.
            CancellationTokenSource cts = new CancellationTokenSource();

            // A blocking collection that can hold no more than 100 items at a time.
            BlockingCollection<int> numberCollection = new BlockingCollection<int>(100);

            // Set console buffer to hold our prodigious output.
            Console.SetBufferSize(80, 2000);

            // The simplest UI thread ever invented.
            Task.Factory.StartNew(() =>
                {
                    if (Console.ReadKey().KeyChar == 'c')
                        cts.Cancel();
                });

            // Start one producer and one consumer.
            Task.Factory.StartNew(() => NonBlockingConsumer(numberCollection, cts.Token));
            Task.Factory.StartNew(() => NonBlockingProducer(numberCollection, cts.Token));


            Console.WriteLine("Press the Enter key to exit.");
            Console.ReadLine();
        }

        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. Press the Enter key to exit.");
        }

        static void NonBlockingProducer(BlockingCollection<int> bc, CancellationToken ct)
        {
            int itemToAdd = 0;
            bool success = false;

            do
            {
                // Cancellation causes OCE. We know how to handle it.
                try
                {
                    // A shorter timeout causes more failures.
                    success = bc.TryAdd(itemToAdd, 2, ct);
                }
                catch (OperationCanceledException)
                {
                    Console.WriteLine("Add loop canceled.");
                    // Let other threads know we're done in case
                    // they aren't monitoring the cancellation token.
                    bc.CompleteAdding();
                    break;
                }

                if (success)
                {
                    Console.WriteLine(" Add:{0}", itemToAdd);
                    itemToAdd++;
                }
                else
                {
                    Console.Write(" AddBlocked:{0} Count = {1}", itemToAdd.ToString(), bc.Count);
                    // Don't increment nextItem. Try again on next iteration.

                    //Do something else useful instead.
                    UpdateProgress(itemToAdd);
                }

            } while (itemToAdd < inputs);

            // No lock required here because only one producer.
            bc.CompleteAdding();
        }

        static void UpdateProgress(int i)
        {
            double percent = ((double)i / inputs) * 100;
            Console.WriteLine("Percent complete: {0}", percent);
        }
    }
}

请参见

参考

System.Collections.Concurrent

概念

BlockingCollection 概述