在通用生产者/消费者中使用任务
我正在尝试使用 BlockingQueue 构建通用生产者/消费者。
我希望它尽可能多线程或并行,但又不占用所有计算机资源。
假设我们有一个生产者,将消费者作为线程更好还是让一个消费者作为线程更好 消费数据时的任务?
while(true)
{
queue.TryTake(...) { Task.Factory.StartNew(...); }
}
或
Thread t = new Thread(Consumer.Start);
I am trying to build a generic producer/ consumer with BlockingQueue.
i want it to be as multi threaded or parallel as possible yet not eating all of the computer resources.
let say we have one producer, is it better to have consumers as threads or one consumer with
Tasks when consuming the data ?
while(true)
{
queue.TryTake(...) { Task.Factory.StartNew(...); }
}
or
Thread t = new Thread(Consumer.Start);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许是一种组合。
您的第一个提案存在这样的问题:当队列填满时,您将启动很多任务,甚至可能太多。
第二个仅使用 1 个线程(我想这就是您的意思)。
您可能应该启动 N 个消费者并为 N 制定一些策略。这在很大程度上取决于工作量、I/O 的使用等。
可能的策略是
您也可以使用任务(带有 LongRunning 选项)而不是线程。
Maybe a combination.
Your first proposal has the problem that when the Queue is well filled you will start a lot of tasks, maybe too many.
The second one would use only 1 Thread (I assume that's what you mean here).
You should probably start N consumers and device some strategy for N. It depends very much on the amount of work, use of I/O etc.
Possible strategies are
And you might as well use a Task (with LongRunning option) instead of a Thread.
如果您担心计算机资源,解决方案可能是创建一个工作线程池,所有线程都监视任务队列,一旦任务放入队列并且工作线程空闲,它就会从队列中读取它并开始工作?
这应该很容易设置。
//丹尼尔
If you are worried about computer resources a solution could be to do a pool of worker threads that all monitors the task queue and as soon as a task is put on queue and a worker thread is free it reads the it from the queue and starts working?
That should be quite easy to set up.
//daniel