1.Task的三种启动方式
Task.Run(() => this.DoSomethingLong("btnTask_Click")); TaskFactory taskFactory = Task.Factory; taskFactory.StartNew(() => DoSomethingLong("btnTask_Click")); new Task(() => DoSomethingLong("btnTask_Click")).Start();
2.WhenAny、WhenAll、WaitAny、WaitAll、CintinueWith
List<Task> taskList = new List<Task>(); taskList.Add(Task.Run(() => this.Coding("jj", "Client"))); taskList.Add(Task.Run(() => this.Coding("vv", "Server"))); Task.WhenAny(taskList.ToArray()).ContinueWith(t => //不卡界面 { Console.WriteLine("得意一下"); }); Task.WhenAll(taskList.ToArray()).ContinueWith(t => //不卡界面 { Console.WriteLine("部署环境,联调测试"); }); //一个业务查询操作有多个数据源,首页-多线程并发,拿到全部数据后才能返回 waitAll //一个商品搜索操作多个数据源,只要一个结果即可 --waitAny Task.WaitAny(taskList.ToArray());//会阻塞当前线程,等着某个线程完成后,会进入到下一行,卡界面 Task.WaitAny(taskList.ToArray(), 1000);//也有限时等待 Console.WriteLine("完成某个里程碑"); Task.WaitAll(taskList.ToArray(), 1000);//限时等待 Task.WaitAll(taskList.ToArray());//会阻塞当前线程,等待全部任务都完成后,才进入下一行 卡界面 Console.WriteLine("告诉甲方验证,上线使用");
3.限制线程池中的数量
List<int> list = new List<int>(); for(int i = 0; i < 1000; i++) { list.Add(i); } Action<int> action1 = i => { Console.WriteLine(""); }; List<Task> taskList1 = new List<Task>(); foreach (var i in list) { int k = i; taskList1.Add(Task.Run(() => action1.Invoke(k))); if (taskList1.Count > 10) { Task.WaitAny(taskList1.ToArray()); taskList1 = taskList1.Where(t => t.Status != TaskStatus.RanToCompletion).ToList(); } } Task.WhenAll(taskList1.ToArray());
4.判断线程池中哪个线程先完成了
List<Task> taskList2 = new List<Task>(); TaskFactory taskFactory1 = new TaskFactory(); taskList2.Add(taskFactory1.StartNew(o => Console.WriteLine("线程1"), "线程1")); taskList2.Add(taskFactory1.StartNew(o => Console.WriteLine("线程2"), "线程2")); taskFactory1.ContinueWhenAny(taskList2.ToArray(), t => { Console.WriteLine(t.AsyncState);//t.AsyncState就是我们创建时线程时传的参数;这样就可以知道是哪个线程先完成了 });
5.如何进行回调
Task.Run(() => Console.WriteLine("jj")).ContinueWith(s => Console.WriteLine("i"));
6.如何保证执行完后的逻辑顺序
taskList.Add(Task.WhenAll(taskList.ToArray())); Console.WriteLine("联调测试"); Task.WaitAll(taskList.ToArray()); Console.WriteLine("验收");
7.Delay延迟的使用
Task.Delay(100);//延迟 不会卡主线程 Thread.Sleep(100); //等待 卡 Task.Delay(2000).ContinueWith(t => { Console.WriteLine("延迟之后执行的逻辑"); }); //delay相当于以下的逻辑 Task.Run(() => { Thread.Sleep(2000); Console.WriteLine("延迟之后执行的逻辑"); });
8.Parallel的使用,并发编程,一次启动多个线程
Parallel.Invoke(() => Console.WriteLine("ee") , () => Console.WriteLine("rr")); Parallel.For(0, 5, i => Console.WriteLine($"ee{i}"));//启动多个线程 Parallel.ForEach(new string[] { "0", "1" }, i => Console.WriteLine(i));//启动多个线程
9.控制Parallel中的并发数量
//可控制并发数量 ParallelOptions parallelOptions = new ParallelOptions(); parallelOptions.MaxDegreeOfParallelism = 3; Parallel.For(0, 10, parallelOptions, i => Console.WriteLine());
10.Parallel中的break与stop
ParallelOptions parallelOptions1 = new ParallelOptions(); parallelOptions1.MaxDegreeOfParallelism = 5; Parallel.For(0, 40, parallelOptions1, (i, state) => { if (i == 15) { state.Break();//结束Parallel当此操作,等于continue;如果是主线程,等于Parallel都结束了 return;//必须带上 } if (i == 20) { state.Stop();//结束Parallel全部操作,等于break return;//必须带上 } });
标签:Task,Console,学习,taskList,线程,WriteLine,Parallel From: https://www.cnblogs.com/guoxu486/p/18360047