托管线程一般建议 #3
一般性建议#3 中的这句话是什么意思?
不控制worker的执行 主程序中的线程(使用 例如,事件)。 相反,设计 你的程序,以便工作线程 负责等到下班 可用,执行它,并且 通知程序的其他部分 等结束了。 如果你的工作线程 不要阻塞,考虑使用线程 池线程。 Monitor.PulseAll 是 在工人的情况下有用 线程阻塞。
有人可以举例说明吗?
-- 来源:MSDN - 托管线程最佳实践
What does this from General Recommandation #3 mean?
Don't control the execution of worker
threads from your main program (using
events, for example). Instead, design
your program so that worker threads
are responsible for waiting until work
is available, executing it, and
notifying other parts of your program
when finished. If your worker threads
do not block, consider using thread
pool threads. Monitor.PulseAll is
useful in situations where worker
threads block.
Can someone explain by examples please?
-- Source: MSDN - Managed Threading Best Practices
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为这个建议也可能是指创建一个线程,然后在其上重复调用诸如
Suspend
和Resume
之类的方法来控制它的运行时间。通常认为更好的形式是让线程执行一个
while
循环,该循环包含某种锁定原语(例如ManualResetEvent
或一些类似的原语),以便在出现问题时向线程发出信号。还有更多工作要做。I think this recommendation may also be referring to creating a thread, and then repeatedly calling methods like
Suspend
andResume
on it to control when it runs.It is generally considered better form to have the thread execute a
while
loop that contains some kind of locking primitive (like aManualResetEvent
or some similar primitive) to signal the thread when there is more work to be done.我认为这意味着您不应该手动创建工作线程来处理任务(例如保存文件),而是拥有一个适当的系统(或使用 ThreadPool.QueueUserWorkItem),您可以在其中将任务/作业排入队列,并且现有工作人员正在等待任务到达(可能使用监视器 Wait 或 AutoResetEvent)。 这样做意味着您可以重复使用线程,而不必不断创建和销毁它们。
.NET 4.0 有一个新的内置 Task 类具有一堆支持类,使这种编程风格变得更容易,这样您就不必在每个项目中重新发明它。
I take this to mean that you shouldn't manually create worker threads to handle tasks (e.g. save a file), but rather have a system in place (or use ThreadPool.QueueUserWorkItem) where you can enqueue a task/job and an existing worker is waiting for a task to arrive (perhaps using a monitor Wait or an AutoResetEvent). Doing it this way means you can re-use threads rather than having to constantly create and destroy them.
.NET 4.0 has a new built-in Task class with a bunch of supporting classes to make this style of programming easier so that you don't have to re-invent this in each project.
好吧,基本上有两种方法可以将工作分配给工作线程。 第一个是将工作项存储在队列中。 当您有工作要做时,您可以将其推入队列并向工作人员发出信号。 您的工作线程看起来像这样:
根据建议,这是您应该采取的方法。
另一种方法是维护工作人员队列。 当您需要完成工作时,您可以从队列中获取一个工作人员(如果队列为空,则创建一个工作人员)并告诉它运行该项目。 第二种方法更难以编码并且通常效率较低。
Well, there are basically two ways you can dole out work to your worker threads. The first is to store work items in a queue. When you have work to do, you push it onto the queue and signal the workers. Your worker threads would look something like this:
This is the approach you're supposed to take, according to the recommendation.
The other approach is to maintain a queue of workers. When you have work that you need to do, you grab a worker from the queue (or create one if its empty) and tell it to run the item. This second approach is more difficult to code and is generally less efficient.