我需要澄清ThreadPool和托管线程C# [英] I need clarification on ThreadPool and Managed Thread(s) C#

查看:60
本文介绍了我需要澄清ThreadPool和托管线程C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我的有限理解中,如果创建了最佳线程数并且正在使用(工作)最佳线程数,则ThreadPool会创建最佳线程数并将进入的工作"排队.
这与创建多线程消费者生产者队列不是一回事吗?如果是这样,创建一个托管有工作/任务"IF"的队列的托管同步线程的需求/目的是什么,我可以使用ThreadPool来完成相同的事情吗?

预先感谢,
-DA

From my limited understanding ThreadPool creates the optimal number of threads and queues incoming "work" if the the optimal number of threads have been created and are in use (working)...

Isn''t this the exact same thing as creating a multiThreaded consumer-producer queue? If so what''s the need/purpose of creating a managed synchronized thread w/ a queue holding the work/task "IF" I can use a ThreadPool and accomplish the same thing?

Thanks in advance,
-DA

推荐答案

不,当然,这些是完全不同的东西.线程池只是自动化的线程重用机制.至于线程同步中使用的队列,如何获取线程并不重要.那不是很明显吗?线程是线程;当您设置线程的属性并已经使用它时,它的工作方式完全相同,而与获取它的方式无关.

排队机制和线程间调用在提示与技巧"中有详细说明.技巧文章"用于线程通信和线程间的简单阻止队列调用" [ ^ ],并附有源代码和用法示例.一种有趣的情况是使用委托实例作为队列元素,这使得调用机制极为灵活,类似于WPF和System.Windows.Forms的UI线程使用的调用机制.
—SA
No, of course these are absolutely different things. Thread pool is just the automated thread re-use mechanism. As to the queues used in thread synchronization, it does not really matter how the threads were obtained. Isn''t that obvious? Threads are threads; when you set up the thread''s properties and are using it already, it works in exact same way, not matter the way you obtained it.

The queuing mechanism and inter-thread invocation is explained in detail in my Tips & Tricks article "Simple Blocking Queue for Thread Communication and Inter-thread Invocation"[^], complete with source code and usage sample. One interesting case is using delegate instances as queue elements, which makes the mechanism of invocation extremely flexible, similar the invocation mechanism used with UI threads with WPF and System.Windows.Forms.

—SA


这篇关于我需要澄清ThreadPool和托管线程C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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