You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
varnums= Enumerable.Range(0,10);//生成一组整数varquery=fromninnumsselectnew{
tid = Thread.CurrentThread.ManagedThreadId,
num = n
};foreach(var item in query){
Console.WriteLine(item);}
2.PLINQ查询操作(并行)
varnums= Enumerable.Range(0,10000);//生成一组整数varquery=fromnin nums.AsParallel()//AsParallel()这个是扩展方法,可以将串行的代码转为并行selectnew{
tid = Thread.CurrentThread.ManagedThreadId,
num = n
};foreach(var item in query){
Console.WriteLine(item);}
varnums=newint[]{10,8,7,3,4};varquery=fromnin nums.AsParallel().AsOrdered().AsSequential()//.AsUnordered()//.AsOrdered()selectnew{
tid = Thread.CurrentThread.ManagedThreadId,
num = n
};foreach(var item in query){
Console.WriteLine(item);}
The text was updated successfully, but these errors were encountered:
TPL(Task Parallel Library)
一:并行计算:
Parallel
各种使用方法1.并行计算的基础使用
2.重载方法 1:中断的出现的
bug
public static ParallelLoopResult For(int fromInclusive, int toExclusive, Action<int, ParallelLoopState> body);
3.重载方法 2:可以指定参与线程的个数:
public static ParallelLoopResult For(int fromInclusive, int toExclusive, ParallelOptions parallelOptions, Action<int> body);
4.
Parallel.For
:使用数组可以做累加运算5.
Parallel.ForEach
:可以遍历字典6.
Parallel.Invoke
并行计算方法二、
PLINQ
为什么使用
PLINQ
?也就是在LINQ
查询中,为了提高效率,可以使用并行版本1.普通
LINQ
查询2.
PLINQ
查询操作(并行)3.扩展方法:
AsOrdered()
就是按照原始顺序排序。AsOrdered()
不等于我们串行编程中的OrderBy,AsSequential()
就是把PLINQ
转成LINQ
The text was updated successfully, but these errors were encountered: