C#并行庫Parallel類介紹
Parallel.Invoke
這個函數(shù)的功能和Task有些相似,就是并發(fā)執(zhí)行一系列任務(wù),然后等待所有完成。和Task比起來,省略了Task.WaitAll這一步,自然也缺少了Task的相關(guān)管理功能。它有兩種形式:
Parallel.Invoke( params Action[] actions); Parallel.Invoke(Action[] actions,TaskManager manager,TaskCreationOptions options);
第二種方式可以自定義一個TaskManager對任務(wù)的執(zhí)行線程進(jìn)行管理(第一種方式用的是默認(rèn)的TaskManager.Default)。
示例如下:
static void Main(string[] args)
{
var actions = new Action[]{
() => ActionTest("test 1"),
() => ActionTest("test 2"),
() => ActionTest("test 3"),
() => ActionTest("test 4")};
Console.WriteLine("Parallel.Invoke 1 Test");
Parallel.Invoke(actions);
Console.WriteLine();
Console.WriteLine("Parallel.Invoke 2 Test");
Parallel.Invoke(actions, new TaskManager(new TaskManagerPolicy(1, 1, 2)), TaskCreationOptions.None);
}
static void ActionTest(object value)
{
Console.WriteLine(">>> thread:{0}, value:{1}",
Thread.CurrentThread.ManagedThreadId, value);
}輸出結(jié)果如下:
Parallel.Invoke 1 Test
>>> thread:3, value:test 1
>>> thread:3, value:test 2
>>> thread:4, value:test 3
>>> thread:5, value:test 4
Parallel.Invoke 2 Test
>>> thread:7, value:test 1
>>> thread:7, value:test 2
>>> thread:8, value:test 3
>>> thread:7, value:test 4
可見,由于第二次指定了最多只能使用兩個線程來執(zhí)行,故只有兩個Task并發(fā)執(zhí)行。
Parallel.ForEach和Parallel.For
Parallel.ForEach和Parallel.For用得要更加廣泛一些,他可以根據(jù)一個數(shù)據(jù)源來生成一些任務(wù)(Parallel.Invoke需要事先生成這些任務(wù)),同時(shí)并發(fā)執(zhí)行這些任務(wù)。基本示例如下:
static void Main(string[] args)
{
var data = new object[] { "test 1", "test 2", "test 3" };
Console.WriteLine("Parallel.ForEach Test");
Parallel.ForEach(data, item => ActionTest(item));
Console.WriteLine();
Console.WriteLine("Parallel.For Test");
Parallel.For(0, data.Length, index => ActionTest(data[index]));
}這兩個函數(shù)都有多種重載形式,提供了許多控制功能,由于用得不是很多,這里就不一一介紹了。但有一點(diǎn)不是很好:如果需要用TaskManager的話,不得不用那最復(fù)雜的那一種形式。希望最終版本的時(shí)候會提供更合理的重載形式,畢竟TaskManager還是很常用的(雖然目前的TaskManager功能薄弱了點(diǎn)),而那些復(fù)雜的參數(shù)不是很常用的。因此,這里提供了兩個常用的擴(kuò)展方法的封裝:
public static class ParallelExtend
{
public static void ParallelForEach<T>(this IEnumerable<T> source, Action<T> hanlder)
{
Parallel.ForEach(source, hanlder);
}
public static void ParallelForEach<T>(this IEnumerable<T> source, Action<T> hanlder, TaskManagerPolicy policy)
{
using (var manager = new TaskManager(policy))
{
Parallel.ForEach(source,
() => 0,
(item, index, state) => hanlder(item),
local => { },
manager,
TaskCreationOptions.None);
}
}
}通過擴(kuò)展方法的方式用起來還是比較方便的。
到此這篇關(guān)于C#并行庫Parallel類的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
算法證明每一位都相同十進(jìn)制數(shù)不是完全平方數(shù)
這篇文章主要為大家介紹了算法證明每一位都相同十進(jìn)制數(shù)不是完全平方數(shù)的過程論述,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
C#利用System.Threading.Thread.Sleep即時(shí)輸出信息的詳解
本篇文章是對C#利用System.Threading.Thread.Sleep即時(shí)輸出信息進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
C# DateTime與時(shí)間戳轉(zhuǎn)換實(shí)例
本篇文章主要介紹了C# DateTime與時(shí)間戳轉(zhuǎn)換實(shí)例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06
C#使用selenium實(shí)現(xiàn)操作瀏覽器并且截圖
這篇文章主要為大家詳細(xì)介紹了C#如何使用selenium組件實(shí)現(xiàn)操作瀏覽器并且截圖,文中的示例代碼簡潔易懂,有需要的小伙伴可以參考一下2024-01-01
關(guān)于C# 4.0新特性“缺省參數(shù)”的實(shí)現(xiàn)詳解
這篇文章主要給大家介紹了關(guān)于C# 4.0新特性“缺省參數(shù)”的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用C# 4.0具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06

