C# Task.WhenAll Parallel
来源:互联网 发布:linux u 编辑:程序博客网 时间:2024/06/05 04:01
- Thread.Sleep and non-Async method will block the thread
- await Task.Delay or await Async method will delay the task, but won’t block the thread. The usage of thread will be handed over to other tasks.
- By default, the system has 4 threads, but may run 60 tasks in parallel. So the following codes will finish in 1 sec using Task.Delay, but more time using Thread.Sleep.
using System;using System.Collections.Generic;using System.Linq;using System.Threading;using System.Threading.Tasks;namespace SystemTasks{ class TestTask { public async static Task<long> TestSyncIth(int ith) { Console.WriteLine("Start TestSyncIth " + ith); Thread.Sleep(1000); //Could not finish in 1 sec //await Task.Delay(1000); //Finish in 1 sec Console.WriteLine("Finish TestSyncIth " + ith); return ith; } public async static Task StartAsync(int option) { List<int> nums = new List<int>(); for (int i = 60; i >= 1; --i) nums.Add(i); var tasks = nums.Select(TestSyncIth).ToArray(); await Task.WhenAll(tasks); foreach(var cur in tasks) { if (cur.Status == TaskStatus.RanToCompletion) { long res = cur.Result; Console.WriteLine("StartAsync " + res); } } Console.WriteLine("All Finish"); } static void Main() { StartAsync(0).ConfigureAwait(false).GetAwaiter().GetResult(); return; } }}
Several strategies to improve the above non-async codes:
1. threadpool.setminthreads
2. Codes like the following:
using System;using System.Collections.Generic;using System.Linq;using System.Threading;using System.Threading.Tasks;namespace SystemTasks{ class TestTask { public async static Task<long> TestSyncIth(int ith) { Console.WriteLine("Start TestSyncIth " + ith); Thread.Sleep(1000); //Could not finish in 1 sec //await Task.Delay(1000); //Finish in 1 sec Console.WriteLine("Finish TestSyncIth " + ith); return ith; } public async static Task StartAsync(int option) { var tasks = new List<Task<long>>(); for (int i = 60; i >= 1; --i) { int cur = i; var t = Task.Factory.StartNew(async () => await TestSyncIth(cur), TaskCreationOptions.LongRunning); tasks.Add(t.Unwrap()); } await Task.WhenAll(tasks); foreach(var cur in tasks) { if (cur.Status == TaskStatus.RanToCompletion) { long res = cur.Result; Console.WriteLine("StartAsync " + res); } } Console.WriteLine("All Finish"); } static void Main() { StartAsync(0).ConfigureAwait(false).GetAwaiter().GetResult(); return; } }}
0 0
- C# Task.WhenAll Parallel
- C# Task.WhenAll Parallel
- Parallel task in C# 4.0
- C# Task WhenAny和WhenAll 以及TaskFactory 的ContinueWhenAny和ContinueWhenAll的实现
- C#并行开发_Thread/ThreadPool, Task/TaskFactory, Parallel
- 改善C#程序的建议10:用Parallel简化Task
- C#并行开发_Thread/ThreadPool, Task/TaskFactory, Parallel
- C#并行开发_Thread/ThreadPool, Task/TaskFactory, Parallel
- Task Parallel.For、Parallel.ForEach、Parallel.Invoke
- 【C#】48. Task WhenAll、WhenAny 创建任务的任务
- Task Parallel Library ( TPL)学习
- Task+ConcurrentQueue+Parallel多线程编程
- 【C#】51. Await 处理并行任务(WhenAll)以及Task.Delay()
- c# Parallel并行运算
- C#并行编程-Parallel
- C# Parallel并行
- C#并行编程-Parallel
- C# Parallel用法
- CC2541 CC2540 之自定义MAC地址读取
- php + MongoDB + Sphinx 实现全文检索 (二)
- python维护代理池脚本
- HDU5997 rausen loves cakes
- 多米音乐登录注册抓包实验
- C# Task.WhenAll Parallel
- ES运维总结
- 数据结构--学生个人信息管理
- Android通过Mapping文件retrace混淆后的堆栈
- 推荐系统——关联规则
- Spring三种实例化bean的方式
- 12月英语总结
- 长恨歌
- Spring 核心框架体系结构