生产者 - 消费者在队列为空时等待?

时间:2011-12-31 17:40:29

标签: c# queue task producer-consumer

我有一个需要按顺序处理的工作项列表。有时列表将是空的,有时它将有一千个项目。一次只能按顺序处理一个。目前我正在做以下哪些对我来说看起来很愚蠢,因为我在消费者任务中使用Thread.Sleep等待100ms,然后检查列表是否为空。这是标准的做法,还是我完全错了。

public class WorkItem
{

}

public class WorkerClass
{
    CancellationTokenSource cts = new CancellationTokenSource();
    CancellationToken ct = new CancellationToken();

    List<WorkItem> listOfWorkItems = new List<WorkItem>();

    public void start()
    {
        Task producerTask = new Task(() => producerMethod(ct), ct);
        Task consumerTask = new Task(() => consumerMethod(ct), ct);

        producerTask.Start();
        consumerTask.Start();
    }

    public void producerMethod(CancellationToken _ct)
    {

        while (!_ct.IsCancellationRequested)
        {
            //Sleep random amount of time
            Random r = new Random();
            Thread.Sleep(r.Next(100, 1000));

            WorkItem w = new WorkItem();
            listOfWorkItems.Add(w);
        }
    }

    public void consumerMethod(CancellationToken _ct)
    {

        while (!_ct.IsCancellationRequested)
        {
            if (listOfWorkItems.Count == 0)
            {
                //Sleep small small amount of time to avoid continuously polling this if statement
                Thread.Sleep(100);
                continue;
            }

            //Process first item
            doWorkOnWorkItem(listOfWorkItems[0]);

            //Remove from list
            listOfWorkItems.RemoveAt(0);
        }
    }

    public void doWorkOnWorkItem(WorkItem w)
    {
        // Do work here - synchronous to execute in order (10ms to 5min execution time)
    }

}

非常感谢。

由于

2 个答案:

答案 0 :(得分:4)

使用BlockingCollection。它会进行非繁忙的等待。

有关简单示例,请参阅https://stackoverflow.com/a/5108487/56778。或者http://www.informit.com/guides/content.aspx?g=dotnet&seqNum=821了解更多细节。

答案 1 :(得分:2)

您可以使用BlockingCollection<T> Class