使用BlockingCollection替换队列。 [英] Using BlockingCollection to replace queue.

查看:97
本文介绍了使用BlockingCollection替换队列。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最初我用队列来解决我的问题。该应用程序是并发调用。每次只能拨打4个电话。(MaximumChannels)

Originally I used queue to solve my problem. The application is a concurrent calls. Each time it only can take 4 calls.(MaximumChannels)

public static int m_ActiveCallCount = 0;
public static int MaximumChannels = 4;
public static object m_SyncVar = new object();
public static Queue<AppointmentReminder> m_Queue = new Queue<AppointmentReminder>();

public static void IncrementCallCounters()
{
    Interlocked.Increment(ref m_ActiveCallCount);
}



将大量项目添加到队列后,我运行了代码。


After adding tons of items to the queue, I had the code to run.

while (true)
                {
                    AppointmentReminder reminder = null;
                    if (m_ActiveCallCount < MaximumChannels)
                    {
                        lock (m_SyncVar)
                        {
                            // Make sure we have values to read
                            if (m_Queue.Count > 0)
                            {
                                reminder = m_Queue.Dequeue();
                            }
                        }
                        if (reminder != null)
                        {
                            Dial dial = new Dial(tServer);
                            Thread oThread = new Thread(() => dial.RunScript(reminder));
                            IncrementCallCounters();
                            oThread.Start();
                        }
                    }



现在我想使用BlockingCollection http://msdn.microsoft.com/en-us/library/dd267312(v = vs.110).aspx [ ^ ]替换队列。我遇到的问题是我不知道如何在这里使用BlockingCollection类来引用MaximumChannels来应用边界数。



我需要代码提示。 />
谢谢。


Now I want to use BlockingCollection http://msdn.microsoft.com/en-us/library/dd267312(v=vs.110).aspx[^] to replace queue. The problem I have is that I am not sure how to apply the boundary number herein referring to "MaximumChannels" with BlockingCollection class.

I need the code hint.
Thanks.

推荐答案

请参阅我对该问题的评论。即使我不是100%清楚,但这看起来像生产者 - 消费者模式的一般问题:[抱歉,我们遇到了CodeProject帖子中锚点断裂的问题;请使用:

http://en.wikipedia.org/wiki/Producer%E2%80%93consumer_problem ;

抱歉给您带来不便]。



班级 BlockingCollection< T> 提供边界(不是与绑定的概念混淆:
Please see my comment to the question. Even though it is not 100% clear to me, but this looks like a general problem of the producer-consumer pattern: [Sorry, we are experiencing the problem of broken anchors in CodeProject posts; please use:
http://en.wikipedia.org/wiki/Producer%E2%80%93consumer_problem;
sorry for the inconvenience].

The class BlockingCollection<T> offers bounding (not to be confused with the notion of binding):
MSDN 告诉我们:
MSDN tells us:

BlockingCollection< t>是一个线程安全的集合类,它提供以下内容:

[...]

一个有界集合,当集合已满或空时阻止Add和Take操作。

BlockingCollection<t> is a thread-safe collection class that provides the following:
[…]
A bounded collection that blocks Add and Take operations when the collection is full or empty.

请注意这些定义边界的构造函数:

http://msdn.microsoft.com/en-us/library/dd267301(v = vs.110).aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/dd267306(v=vs.110)。 aspx [ ^ ]

和该成员: http://msdn.microsoft.com/en-us/library/dd287159(v = vs.110).aspx [ ^ ]。



另请参阅: 如何:添加边界和阻止功能集合



关于替换(为什么?),考虑添加名为<$的方法c $ c> Skip * 。



-SA

Please pay attention for these constructors defining bounding:
http://msdn.microsoft.com/en-us/library/dd267301(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/dd267306(v=vs.110).aspx[^]
and for this member: http://msdn.microsoft.com/en-us/library/dd287159(v=vs.110).aspx[^].

See also: How to: Add Bounding and Blocking Functionality to a Collection.

As to the "replacing" (why?), consider adding and the methods named Skip*.

—SA


这篇关于使用BlockingCollection替换队列。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆