线程与并行处理 [英] Threading vs. Parallel Processing

查看:67
本文介绍了线程与并行处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Microsoft .NET 4.0向其框架引入了新的并行增强".我想知道制作使用标准System.Threading函数的应用程序与新的并行增强功能之间的区别是什么.

解决方案

并行扩展和常规线程之间最重要的区别可能是控制流.

使用new Thread(...)ThreadPool.QueueUserWorkItem创建的线程将在完全不确定的时间点终止.如果您编写此代码:

ThreadPool.QueueUserWorkItem(() =>
    {
        Thread.Sleep(1000);
        Console.WriteLine("Work Finished");
    });
Console.WriteLine("Item Queued");

文本Item Queued将立即出现,并且Work Finished将在大约1秒钟的延迟后打印.

另一方面,如果您使用并行扩展写类似的东西:

Parallel.For(0, 10, i =>
    {
        Thread.Sleep(1000);
        Console.WriteLine("Test {0}", i);
    });
Console.WriteLine("Finished");

在这种情况下,您会看到发生任何事情之前要延迟1秒,然后以随机顺序排列一系列测试"消息,然后然后输入文本Finished. /p>

换句话说,并行运行任务实际上并不会改变程序流程.它将在不同的线程上运行不同的任务,以便可以在多个CPU内核上执行这些任务,以提高程序的整体吞吐量,但是就典型的程序员而言,这些任务并不是真正在后台"运行就像使用线程一样.您无需更改程序的结构,也不必执行任何特殊的工作即可在工作完成时得到通知.您无法控制在并行块内部中发生的事情,但是您知道该块在所有并行任务完成之前不会返回控制权.

尽管并行扩展对此非常有用,但值得一提的是,当您实际上需要在后台运行任务(例如实现调度程序或委派给工作人员)时,PX毫无用处线程以保持UI响应.您仍然需要使用线程或异步组件.

Microsoft .NET 4.0 introduces new "parallel enhancements" to its framework. I am wondering what the difference between making an application that uses the standard System.Threading functions versus the new parallel enhancements.

解决方案

Probably the most important difference between the Parallel Extensions and regular threading is the control flow.

A thread, created using new Thread(...) or ThreadPool.QueueUserWorkItem will terminate at a completely indeterminate point in time. If you write this code:

ThreadPool.QueueUserWorkItem(() =>
    {
        Thread.Sleep(1000);
        Console.WriteLine("Work Finished");
    });
Console.WriteLine("Item Queued");

The text Item Queued will appear right away, and Work Finished will be printed after about a 1 second delay.

On the other hand, if you write something similar using parallel extensions:

Parallel.For(0, 10, i =>
    {
        Thread.Sleep(1000);
        Console.WriteLine("Test {0}", i);
    });
Console.WriteLine("Finished");

What you'll see in this case is a delay of 1 second before anything happens, then a slew of "Test" messages in a random order, and then the text Finished.

In other words, running tasks in parallel does not actually alter the program flow. It will run different tasks on different threads so that they can be executed on multiple CPU cores, in order to improve overall throughput of the program, but as far as the typical programmer is concerned, these tasks aren't really running in the "background" as they would be with a thread. You don't have to change your program's structure or do anything special to be notified when the work completes. You have no control over what happens inside the parallel block, but you do know that the block will not return control until all parallel tasks are complete.

Although Parallel Extensions are great for this, it bears mentioning that PX are of no use whatsoever when you actually need to run a task in the background, such as implementing scheduler, or delegating to a worker thread in order to keep a UI responsive. You still need to use threads or async components for those.

这篇关于线程与并行处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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