并行学习小结
并行学习小结,直接上源代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading.Tasks;
using System.Threading;
using System.Diagnostics;
using System.Collections.Concurrent;
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
UseParallel();
UsePLINQ();
}
/// <summary>
/// PLINQ的基本方法应用集合
/// </summary>
private void UsePLINQ()
{
#region 并行集合运算,Environment.ProcessorCount - 1 给一个处理器出来处理其他任务
ConcurrentDictionary<int, string> cds = new ConcurrentDictionary<int, string>();//并行考虑用的集合
Parallel.For(0, 3000, i =>
{
cds.TryAdd(i, Math.Sqrt(i).ToString());
});
var q1 = from c in cds.Values.AsParallel().WithDegreeOfParallelism(Environment.ProcessorCount - 1)
select c;//直接获取该集合,Environment.ProcessorCount - 1 给一个处理器出来处理其他任务
#endregion
#region 并行常用的运算
ConcurrentBag<int> bags = new ConcurrentBag<int>();//线程安全的集合
var list = ParallelEnumerable.Range(0,3000);//生成并行集合
list.ForAll(i => {
bags.Add(i);
});
Response.Write("bags长度:" + bags.Count+"<br/>");
Response.Write("list整数和:" + list.Sum() + "<br/>");
Response.Write("list最大值:" + list.Max() + "<br/>");
Response.Write("list最小值:" + list.Min() + "<br/>");
Response.Write("list平均值:" + list.Average() + "<br/>");
Response.Write("list默认值:" + list.FirstOrDefault() + "<br/>");
#endregion
#region 先并行分组,在对每组并行查询统计
List<Student> list2 = new List<Student>()
{
new Student()
{
ID = 1,
Name = "jack",
Age = 20
},
new Student()
{
ID = 1,
Name = "mary",
Age = 25
},
new Student()
{
ID = 1,
Name = "joe",
Age = 29
},
new Student()
{
ID = 1,
Name = "Aaron",
Age = 25
}
};
var map = list2.AsParallel().ToLookup(i => i.Age,count=>1);//化简统计
var reduce = from IGrouping<int, int> singleMap
in map.AsParallel()
select new
{
Age = singleMap.Key,
Count = singleMap.Count()
};
reduce.ForAll(i =>{
Response.Write(string.Format("当前Age={0}的人数有:{1}人<br/>", i.Age, i.Count));
});
#endregion
foreach (var o in q1)
{
Response.Write(o + "<br/>");
}
}
/// <summary>
/// Parallel的基本使用方法集合
/// </summary>
private void UseParallel()
{
#region 并行查询和并行输出
List<string> lists = new List<string>();
for (int i = 0; i < 1000; i++)
{
lists.Add(i.ToString());
}
var q1 = from p in lists.AsParallel()
where Convert.ToInt32(p) > 100
select p;// 并行查询
q1.ForAll<string>(p =>
{
Response.Write(p + "<br/>");//并行输出,注意观察输出结果不是有序的
});
#endregion
#region 串行执行任务
Stopwatch sw = new Stopwatch();
sw.Start();
Run1();
Run2();
Response.Write("串行执行时间:" + sw.ElapsedMilliseconds + "<br/>");
#endregion
#region 并行执行任务
sw.Restart();
Parallel.Invoke(Run1, Run2);
Response.Write("并行执行时间:" + sw.ElapsedMilliseconds + "<br/>");
#endregion
#region 并行赋值
List<int> mylist = new List<int>();
Parallel.ForEach(Partitioner.Create(0, 5000), i =>
{
for (int m = i.Item1; m < i.Item2; m++)
{
mylist.Add(m);
}
});
#endregion
#region 并行 退出循环 Break和Stop
List<int> mylist2 = new List<int>();
Parallel.For(0, 5000, (i, state) =>
{
if (mylist2.Count == 50)
{
state.Break();
return;
}
mylist2.Add(i);
});
#endregion
#region 并行抛出异常处理
try
{
Parallel.Invoke(Run1, Run2, Run3);
}
catch (AggregateException ex)
{
foreach (var single in ex.InnerExceptions)
{
Response.Write(single.Message + "<br/>");
}
}
#endregion
}
private void Run1()
{
Thread.Sleep(3000);
}
private void Run2()
{
Thread.Sleep(5000);
}
private void Run3()
{
Thread.Sleep(1000);
}
private void Run4(int ms)
{
Thread.Sleep(ms * 100);
}
}
public class Student {
public int ID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public DateTime CreateTime { get; set; }
}
浙公网安备 33010602011771号