1.Task多线程简单模板
- using System;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- namespace Exercises
- {
- public
class GroupInfo - {
- public
string GroupId { get; set; } - public
string Data { get; set; } - }
- public
class MutilTaskTemplate - {
- //执行此方法,可以看到效果
- public
void Test() - {
- Run();
- }
- public IEnumerable<GroupInfo> GetGroupInfos()
- {
- return
new List<GroupInfo>() { - new GroupInfo(){ GroupId = Guid.NewGuid().ToString(), Data = "what!"},
- new GroupInfo(){ GroupId = Guid.NewGuid().ToString(), Data = "hello!"},
- new GroupInfo(){ GroupId = Guid.NewGuid().ToString(), Data = "world!"},
- new GroupInfo(){ GroupId = Guid.NewGuid().ToString(), Data = "hehe!"}
- };
- }
- public
void Run() - {
- Console.WriteLine("start!");
- var groupInfos = GetGroupInfos();
- List<Task> groupInfoTasks = new List<Task>();
- foreach (var groupInfo in groupInfos)
- {
- var task = new Task((inGroupInfo) =>
- {
- ForEveryGroup(inGroupInfo as GroupInfo);
- }, groupInfo);
- groupInfoTasks.Add(task);
- }
- Parallel.ForEach(groupInfoTasks, (t) => { t.Start(); });
- Task.WaitAll(groupInfoTasks.ToArray());
- Console.WriteLine("over!");
- }
- private
void ForEveryGroup(GroupInfo groupInfo) - {
- Console.WriteLine();
- Console.WriteLine(groupInfo.GroupId);
- //use the groupInfo.Data;
- Console.WriteLine(groupInfo.Data);
- }
- }
- }
执行结果:
2.Task多线程带子线程模板
- using System;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using System.Linq;
- namespace Exercises
- {
- public
class GroupInfo2 - {
- public
string GroupId { get; set; } - public List<RealData> Datas { get; set; }
- }
- public
class RealData - {
- public RealData(string name, int age)
- {
- this.Name = name;
- this.Age = age;
- }
- public
string Name { get; set; } - public
int Age { get; set; } - }
- public
class MutilTaskWithSubTaskTemplate - {
- //执行此方法,可以看到效果
- public
void Test() - {
- Run();
- }
- public IEnumerable<GroupInfo2> GetGroupInfos()
- {
- return
new List<GroupInfo2>() { - new GroupInfo2(){ GroupId = Guid.NewGuid().ToString(), Datas = new List<RealData>(){
- new RealData("daniu",26),new RealData("daniu2",27),new RealData("daniu3",28)} },
- new GroupInfo2(){ GroupId = Guid.NewGuid().ToString(), Datas = new List<RealData>(){
- new RealData("pzdn",26),new RealData("pzdn2",27)} },
- new GroupInfo2(){ GroupId = Guid.NewGuid().ToString(), Datas = new List<RealData>(){
- new RealData("pozi",26),new RealData("pozi2",27),new RealData("pozi3",28)} },
- new GroupInfo2(){ GroupId = Guid.NewGuid().ToString(), Datas = new List<RealData>(){
- new RealData("douzi",26),new RealData("douzi2",27),new RealData("douzi3",28)} }
- };
- }
- public
void Run() - {
- Console.WriteLine("start!");
- var groupInfos = GetGroupInfos();
- int splitNumber = 2; //2条数据一个Task,改变此数值,则可以控制每条子线程的执行数据量
- List<Task> groupInfoTasks = new List<Task>();
- foreach (var groupInfo in groupInfos)
- {
- var task = new Task((inGroupInfo) =>
- {
- int remainData = groupInfo.Datas.Count % splitNumber;
- int splits = groupInfo.Datas.Count / splitNumber + (remainData != 0 ? 1 : 0);
- var tmpGroupInfo = inGroupInfo as GroupInfo2;
- for (int i = 0; i < splits; i++)
- {
- bool flag = (i == splits - 1) && remainData != 0;
- //当前分组的子线程
- var subTask = new Task((inList) =>
- {
- ForEveryGroup(groupInfo.GroupId, inList as IEnumerable<RealData>);
- }, tmpGroupInfo.Datas.Skip(splitNumber * i).Take(flag ? remainData : splitNumber), TaskCreationOptions.AttachedToParent);
- subTask.Start();
- }
- }, groupInfo);
- groupInfoTasks.Add(task);
- }
- Parallel.ForEach(groupInfoTasks, (t) => { t.Start(); });
- try
- {
- Task.WaitAll(groupInfoTasks.ToArray());
- }
- catch (AggregateException e) //AggregateException可以捕获运行中出现的错误
- {
- Console.WriteLine("Caught Exception:" + e.InnerException.Message); //这里显示
- }
- Console.WriteLine("over!");
- }
- private
void ForEveryGroup(string groupId, IEnumerable<RealData> realDatas) - {
- Console.WriteLine(groupId);
- //use the groupInfo.Data;
- Console.WriteLine(realDatas == null);
- foreach (var item in realDatas)
- {
- Console.WriteLine(item.Name + "-" + item.Age);
- }
- }
- }
- }
执行结果:
3.加入超时
可以增加一个等待超时的字段,在Task.WaitAll(groupInfoTasks.ToArray(),timeOut);