C# 最佳线程队列示例/最佳实践
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/346268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best threading queue example / best practice
提问by leora
I have data that needs to be executed on a certain background thread. I have code coming from all other threads that need to call into this. does anyone have a good tutorial or best practice on having a queue for synchronization to support this threading requirement
我有需要在某个后台线程上执行的数据。我有来自需要调用它的所有其他线程的代码。有没有人有关于同步队列以支持此线程要求的好的教程或最佳实践
采纳答案by VonC
You could either:
你可以:
- implement a producer/consumer model(which use not thread-safe generic queue)
- or use in your background thread a synchronized queue, which does comment on the process of Enumerating through a collection.
回答by Dirk Vollmar
This is an interesting article on ThreadPools:
这是一篇关于 ThreadPools 的有趣文章:
For simpler use cases you could also use .Net's own ThreadPool class.
对于更简单的用例,您还可以使用 .Net 自己的 ThreadPool 类。
回答by Michael Haren
One of my favorite solutions to this problem is similar to the producer/consumer
pattern.
对于这个问题,我最喜欢的解决方案之一类似于producer/consumer
模式。
I create a master thread (pretty much my program's Main()
) which holds a blocking queue object.
我创建了一个主线程(几乎是我的程序的Main()
),它包含一个阻塞队列对象。
This master thread spins off several worker threads which simple pop things off the central blocking thread and process them. Since it's a threadsafe blocking queue, the synchronization bits are easy--the TaskQueue.Dequeue()
call will block until a task is enqueued by the producer/main thread.
这个主线程分离出几个工作线程,这些工作线程简单地从中央阻塞线程中弹出并处理它们。由于它是一个线程安全的阻塞队列,同步位很容易——TaskQueue.Dequeue()
调用将阻塞,直到任务被生产者/主线程排队。
You can dynamically manage the number of workers you want or fix it according to a configuration variable--since they're all just popping things off the queue, the number of workers doesn't add any complexity.
您可以动态管理您想要的工作人员数量或根据配置变量修复它——因为他们都只是从队列中弹出东西,工作人员的数量不会增加任何复杂性。
In my case, I have a service which processes several different types of tasks
. I have the queue typed to handle something generic like TaskQueueTask
. Then I subclass that and override the Execute()
method.
就我而言,我有一个服务可以处理几种不同类型的tasks
. 我输入了队列来处理诸如TaskQueueTask
. 然后我将其子类化并覆盖该Execute()
方法。
I've also tried the .NET threadpool approach where in you can throw things into the pool very easily. It was extremely simple to use but also provided little control, and no guarantee of execution order, timing, etc. It's recommended only for light-weight tasks.
我还尝试了 .NET 线程池方法,在这种方法中,您可以非常轻松地将东西放入池中。它使用起来极其简单,但也提供很少的控制,并且不能保证执行顺序、时间等。仅推荐用于轻量级任务。
回答by Mauricio Scheffer
Check out Threading in C#, by Joseph Albahari, very complete reference about multithreading. In particular, he covers producer/consumer queues.
查看约瑟夫·阿尔巴哈里 (Joseph Albahari) 编写的C# 中的线程,这是关于多线程的非常完整的参考。他特别介绍了生产者/消费者队列。
回答by jake.stateresa
You can try this solution. It shows you how to implement the producer-consumer pattern. Has also some explanation on what can be done with it. Like different combinations of the number of producers and consumers.
你可以试试这个解决方案。它向您展示了如何实现生产者-消费者模式。还有一些关于可以用它做什么的解释。就像生产者和消费者数量的不同组合。