Multiple Tasks Z
public static async Task executeParallel<T>(this IEnumerable<T> items, int limit, Func<T, Task> actionMethod)
{
var allTasks = new List<Task>(); //Store all Tasks
var activeTasks = new List<Task>();
foreach (var item in items)
{
if (activeTasks.Count >= limit)
{
var completedTask = await Task.WhenAny(activeTasks);
activeTasks.Remove(completedTask);
}
var task = actionMethod(item);
allTasks.Add(task);
activeTasks.Add(task);
}
await Task.WhenAll(allTasks); // Wait for all task to complete
}
public async Task fun(int processId)
{
await Task.Run( () =>{
Random rand = new Random();
Console.WriteLine("Processing " + processId);
Thread.Sleep(rand.Next(1500));
Console.WriteLine("Done processing - " + processId);
});
} internal async void process(List<int> queue,int limit)
{
await queue.executeParallel(limit, fun);
} https://www.codeproject.com/Tips/1264928/Throttling-Multiple-Tasks-to-Process-Requests-in-C
Multiple Tasks Z的更多相关文章
- 异步数据库查询 Z
Introduction Microsoft .NET 4.5 introduced new "async and await" methods to provide an eas ...
- Async/Await - Best Practices in Asynchronous Programming z
These days there’s a wealth of information about the new async and await support in the Microsoft .N ...
- Android 性能优化(16)线程优化:Creating a Manager for Multiple Threads 如何创建一个线程池管理类
Creating a Manager for Multiple Threads 1.You should also read Processes and Threads The previous le ...
- Threading and Tasks in Chrome
Threading and Tasks in Chrome Contents Overview Threads Tasks Prefer Sequences to Threads Posting a ...
- Creating a Manager for Multiple Threads_翻译
The previous lesson showed how to define a task that executes on a separate thread. If you only want ...
- (转) Written Memories: Understanding, Deriving and Extending the LSTM
R2RT Written Memories: Understanding, Deriving and Extending the LSTM Tue 26 July 2016 When I was ...
- 高级I/O之异步I/O
A synchronous I/O operation causes the requesting process to be blocked until that I/O operation com ...
- Unity 5 Game Optimization (Chris Dickinson 著)
1. Detecting Performance Issues 2. Scripting Strategies 3. The Benefits of Batching 4. Kickstart You ...
- 【论文笔记】多任务学习(Multi-Task Learning)
1. 前言 多任务学习(Multi-task learning)是和单任务学习(single-task learning)相对的一种机器学习方法.在机器学习领域,标准的算法理论是一次学习一个任务,也就 ...
随机推荐
- 【AtCoder】AGC013
AGC013 A - Sorted Arrays 直接分就行 #include <bits/stdc++.h> #define fi first #define se second #de ...
- 【AtCoder】AGC015
AGC015 A - A+...+B Problem #include <bits/stdc++.h> #define fi first #define se second #define ...
- 将input或textarea设置为disabled的样式问题
input:disabled{ -webkit-text-fill-color: #333;//是用来做填充色使用的 -webkit-opacity: 1; color: #333; } textar ...
- input时间表单默认样式修改(input[type="date"])
一.时间选择的种类: HTML代码:选择日期:<input type="date" value="2018-11-15" /> 选择时间:<i ...
- Linux安装Tomcat-Nginx-FastDFS-Redis-Solr-集群——【第五集之网络配置】
还有对第五集的补充:https://www.cnblogs.com/lirenhe/p/10405069.html 1,如果不为这个linux系统或者这台虚拟机配置IP,就不能实现通信.这样的之后安装 ...
- POJ 2455 Secret Milking Machine 【二分】+【最大流】
<题目链接> 题目大意: FJ有N块地,这些地之间有P条双向路,每条路的都有固定的长度l.现在要你找出从第1块地到第n块地的T条不同路径,每条路径上的路段不能与先前的路径重复,问这些路径中 ...
- ACM10.14题解
ACM10.14题解 第一次打周赛,感觉还是比较紧张的,应该开完所有的题再做,而不是硬做,没必要硬杠英语,还是不要抱有侥幸心理,做对一定是完全理解且会,自己小心边界问题,不要瞎交. A:暴力明显不对嘛 ...
- switch反汇编
以下总结为debug模式
- 一种表达式语言的解析引擎JEXL简单使用
Jexl 是一个 Expression Language 的解析引擎, 是为了方便嵌入你的系统或者程序框架的开发中, 他算是实现了 JSTL 中 EL 的延伸版本. 不过也采用了一些 Velocity ...
- 事务(transaction )
事务(Transaction)是访问并可能更新数据库中各种数据项的一个程序执行单元(unit). 事务是恢复和并发控制的基本单位. 事务有4个特性:原子性.一致性.隔离性.持久性.(即ACID) 原子 ...