C#~异步编程再续~大叔所理解的并行编程(Task&Parallel)

时间:2024-11-30 10:36:26

返回目录

并行这个概念出自.net4.5,它被封装在System.Threading.Tasks命名空间里,主要提供一些线程,异步的方法,或者说它是对之前Thread进行的二次封装,为的是让开发人员更方便的调用它,对于异步与多线程我们在之前的几讲里已经介绍过了,今天主要说说并行,并行也可以叫并行计算,即对于一个大任务,使用多个线程去计算它,这可以充分发挥多核CPU的优势,可以说是大事所趋!

先看一下并行编程(并行计算)的图像

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAqIAAAHiCAIAAAC80zlVAAAgAElEQVR4nO2d61NT596wnz8l/0o/9psz/dAPnfaZ2Q/PY+lriRGC0Vptt3vTLWWrGxWtFSUB5CQoIh7q2R1DOIRDQFuLVlAkHBKtrYcpTIf3Q9w0DUlAcmfd616/65prOmTlwDK5u64syE//awkAAAAcyn/p3gEAAAAoFGQeAADAsZB5AAAAx0LmAQAAHAuZBwAAcCxkHgAAwLGQeQAAAMdC5gEAABwLmQcAAHAsZB4AAMCxkHkAAADHQuYBAAAcC5kHAABwLGQeAADAsZB5AAAAx0LmAQAAHAuZBwAAcCxkHgAAwLGQeQAAAMdC5gEAABwLmQcAAHAsZB4AAMCxkHkAAADHQuYBAAAcC5kHAABwLGQeAADAsZB5AAAAx0LmAQAAHAuZBwAAcCxkHgAAwLGQeQAAAMdC5gEAABwLmQcAAHAsZB4AAMCxkHkAAADHQuYBAAAcC5kHAABwLGQeAADAsRQw8y9eL3aEJve0jxVVh96ruI4W+OnR3n2ddy9FnhTuZQUAa5iKv2y59dDnj3xQdUv7sQXzV9fxuVCZH3yQ2HioZ9vJ6P4LD+p7ps8MzXeNJLCgdgzN1/37SWXXeHn9cGlt/1T8ZYFeXAAoNG3BiY9rwttPjh659qi1b7ZzOK79CIP5qPH4XJDMHzj3vfvYwPHgk7PD8UtjT9FiO4fjBy49/Lgm/N3QdCFeXwAoHLM/v/70aO+OptGTvTMXRvUfT1Ct1h+f1Wf+wsCUr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4GQeZBLMvNNfbMoxEYyD/Ig8yAXMi9NMg8CIfMgFzIvTTIPAiHzIJei6tDx4JOTvTMoxMbeGTIP0iDzIBcyL00yDwIh8yAXMi9NMg8CIfMgl2TmG3tnUIgNZB7kQeZBLmRemmQeBELmQS5F1aHafz9p6J1BOZJ5kAaZB7n4/JEDlx9qDw9a5onb0x9U3dK97gAshcyDXI5d+nHXqbva24OWeejKRGltv+51B2ApZB7k0ntv3nM8EgjH6lGGu07dPXbpR93rDsBSyDzI5dmL3z450vvtrSnt+UELDPTEPjnSe2/que51B2ApZB5Ec2tspqxu0N+jP0JYaHe13qm7el/3igOwGjIP0vnHqdEvTt3198QCYXSqM3u6xsvrIguLv+tebgBWQ+ZBOguLv+/tvLPpaF/NtUeB8EygJ4ZOsjY4XR4Y2tk4/OL1ou61BqABMg+wtLS0NDb5rLgmvOlon69hpLJr/NDVySPXH6Gh1lyb3Hfp4faTo5uO9v3Pv25fGZ7Wvb4AtEHmAf5gbPJZV9/jPe1jPn8EjXZn43DLrYdjk884iQfhkHkAAADHQuYBAAAcC5kHAABwLGQeAADAsZB5AAAAx0LmAQAAHAuZBwAAcCxkHgAAwLGQeQAAAMdC5gEAABwLmQcAAHAsZB4AAMCxkHkAAADHQuYBAAAcy/8HV/Iuu8JvHoAAAAAASUVORK5CYII=" alt="" />

对于两个任务,任务1执行需要1秒,任务2执行需要3秒,那么,如果顺序执行,它需要的时间为4秒(1+3),而如果是并行编程,那就运行时间为3秒,即(1和3一起运行,取最长的时间),这就是并行计算的魅力!

下面看两种并行的实现方式

一 Task实现的并行

      #region 并行Task
Console.WriteLine(DateTime.Now);
var task = Task.WhenAll(Task.Run(() => { Thread.Sleep(); }), Task.Run(() => { Thread.Sleep(); }));//多个task并行执行,不阻塞
task.ContinueWith((ctw) =>//当task完成后,执行这个回调
{
Console.WriteLine("并行完成" + DateTime.Now);
});
Console.WriteLine(DateTime.Now);
#endregion

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQU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6yXprWI5fllniJf/Rf44VwccSUk5z/q63sD0eb6Ai1OdD5TTpwR7YU0OdfuwPuq0+33yFOLeYbZifHbe7Izeej2Ffn+nSwh0vFH6ut7AU+8/TJddnuCHYA+8UkvVpVhb0/hpreh14+UfL8ET14u414nnDRp/Cv+1H+rDkvnsOy1ZUeep8XrdxPl7h574VC3Q8Q7X1vUGUs8vBcvIxve9zXnsu+p/H+31Wb5UO+RtVdMuxisrSsVn51UmVdabEmxaFhSW1/U2fr4fpq+LcxUBkMb7vegwWPfDtEb7LsUDwXT4ATYwfF7DC16u76Y97mrerMU08AMc5w+d1M3DRwsjoAAAAABJRU5ErkJggg==" alt="" />

二 Parallel实现的并行

     #region 并行Parallel
Console.WriteLine(DateTime.Now);
Parallel.Invoke(() =>
{
Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
Thread.Sleep();
}, () =>
{
Console.WriteLine(Thread.CurrentThread.ManagedThreadId);
Thread.Sleep();
});
Console.WriteLine(DateTime.Now);
Console.ReadKey(); #endregion

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQUAAABDCAIAAAAagocDAAAEG0lEQVR4nO2dQXIjIQxFfTQfzUdjmWUWuY9nMTUWASS+aGxnOu8tptwgPqLRj93uds3lfr9/fX58fX7cbrfr9XoB+M3gBwADPwAY+AHAwA8ABn4AMPADgNH7oVTUkeU7jU7fUg+ZplE6UkOW562HxCmlpOr4rMjCeYZtNH6o98B7PT0MGnv+xqTKJZWbqFlG5l/QbOo4yFPJ5PjSIMeCH+otLxW1rG6GlB+G8dma6wXj/PuA4ZChWpybp9z3ennCZoLrh+FeDjcmriePbB17MVkdRVlZbzxEyS04k+J5hs14fgj2KehtWoZ1E+jr8cs6Q7z8L/LHuTrgSErZ8wybGfohru8+IKinaW+PHp+dNzujt15PISh9MZnseYbNeN8vNWHNHk/3KRU8nGsaH+QpThRIKev1xKeJBTrZ8wybedL3S6LOU+P16onz9w498alaoJM9z7CZoR9qHpHDdjG+bvQyScVn51UmVdabEmxaFhR6kawa5OD+NICBHwAM/ABg4AcAAz8AGPgBwMAPAEbw/FITyZffcH5Szy95vQAnIbg/7Q3BD3BaxPeHGvwApyXlB64f4OTw/gBg4AcAg++XAIy13z+8LV2Ap8L9aQADPwAY+AHAwA8ABn4AMPADgIEfAAx+/wBgcH8awBB//4Af4FfA83wARtYPmAHOzMLvgV6YHcBr0f2AGeD88P0SgCH+/sFrBzgV3J8GMPADgIEfAAz9+aW4Pe4KWL4UKf/+R9q14QADgvcH7xaEJxVcfMdVe9BIWAK2kfLDljeHoVuyFkrNCKCy5fohVZpicB22xT8Ac3o/ZIuvZD63LJjBG+VJeQ6JzeNNITptwZxxMlmHByILUsX5Y6QP74esJfNqdn1emvb2wR6istdYKn8OX08Pg0YvjVS5pHITNcvI/AuazS4EeSqZPA6zOm9j+/VD8VlOcji2Oe/17Mrrep+m9RQk1u93Nv5IrSj59wHejvRqcW6esjKLvsaXssUP3gp3Lbt8pxf3Zu/3clhAcT3FWa3FH9FRlJX1xkOU3IIzOZz3yBpfxHE/PNbvdfWNCp5O0fwQ7FPQ6yXprWI5fllniJf/Rf44VwccSUk5z/q63sD0eb6Ai1OdD5TTpwR7YU0OdfuwPuq0+33yFOLeYbZifHbe7Izeej2Ffn+nSwh0vFH6ut7AU+8/TJddnuCHYA+8UkvVpVhb0/hpreh14+UfL8ET14u414nnDRp/Cv+1H+rDkvnsOy1ZUeep8XrdxPl7h574VC3Q8Q7X1vUGUs8vBcvIxve9zXnsu+p/H+31Wb5UO+RtVdMuxisrSsVn51UmVdabEmxaFhSW1/U2fr4fpq+LcxUBkMb7vegwWPfDtEb7LsUDwXT4ATYwfF7DC16u76Y97mrerMU08AMc5w+d1M3DRwsjoAAAAABJRU5ErkJggg==" alt="" />

通过上面的图我们可以看到,在进行并行测试时,运行时间为两秒!

返回目录