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
        }

  1. public async Task fun(int processId)
  2. {
  3. await Task.Run( () =>{
  4. Random rand = new Random();
  5. Console.WriteLine("Processing " + processId);
  6. Thread.Sleep(rand.Next(1500));
  7. Console.WriteLine("Done processing - " + processId);
  8. });
  9. }
  10.  
  11.   internal async void process(List<int> queue,int limit)
            {
                await queue.executeParallel(limit, fun);
            }
  12.  
  13. https://www.codeproject.com/Tips/1264928/Throttling-Multiple-Tasks-to-Process-Requests-in-C

Multiple Tasks Z的更多相关文章

  1. 异步数据库查询 Z

    Introduction Microsoft .NET 4.5 introduced new "async and await" methods to provide an eas ...

  2. 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 ...

  3. 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 ...

  4. Threading and Tasks in Chrome

    Threading and Tasks in Chrome Contents Overview Threads Tasks Prefer Sequences to Threads Posting a ...

  5. 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 ...

  6. (转) Written Memories: Understanding, Deriving and Extending the LSTM

    R2RT   Written Memories: Understanding, Deriving and Extending the LSTM Tue 26 July 2016 When I was ...

  7. 高级I/O之异步I/O

    A synchronous I/O operation causes the requesting process to be blocked until that I/O operation com ...

  8. Unity 5 Game Optimization (Chris Dickinson 著)

    1. Detecting Performance Issues 2. Scripting Strategies 3. The Benefits of Batching 4. Kickstart You ...

  9. 【论文笔记】多任务学习(Multi-Task Learning)

    1. 前言 多任务学习(Multi-task learning)是和单任务学习(single-task learning)相对的一种机器学习方法.在机器学习领域,标准的算法理论是一次学习一个任务,也就 ...

随机推荐

  1. Flink--DateSet的Transformation简单操作

    flatMap函数 //初始化执行环境 val env: ExecutionEnvironment = ExecutionEnvironment.getExecutionEnvironment //加 ...

  2. Vijos1983 NOIP2015Day2T3 运输计划 transport LCA

    题目链接Vijos 题目链接UOJ 该博客在博客园的链接 转载一个大佬的题解: 点击这里->大佬题解 下面谈谈我的感悟: 当然写代码也是写的很艰辛: 我力劝C++的同胞们,这题卡常数,Dfs党会 ...

  3. 006 使用SpringMVC开发restful API四--用户信息的修复与删除,重在注解的定义

    一:任务 1.任务 常用的验证注解 自定义返回消息 自定义校验注解 二:Hibernate Validator 1.常见的校验注解 2.程序 测试类 /** * @throws Exception * ...

  4. 054 kafka内部机制

    一:数据格式与数据存储 1.总结 存储在磁盘文件中(index+log) 顺序读写的 基于offset偏移量来管理数据的(主要是读操作) 由分区器根据key值决定数据分布到哪个分区,默认使用hash ...

  5. Java版统计文件中的每个单词出现次数

    正则表达式之Pattern和Matcher,请参见转载博客    http://www.cnblogs.com/haodawang/p/5967219.html 代码实现: import java.i ...

  6. setTimeout/setInterval,属性、连续动画、倒计时的分析

    setTimeout.setInterval环境应用和使用场景 说明:setTimeout属于超时调用, setInterval 属于间隔调用 1,setTimeout超时的使用介绍: var set ...

  7. JAVA首次课堂测试总结

    暑期生活已经结束,新的学期也已经开始,而暑期放假之前约定的JAVA首次课堂测试也如期的到来,本次测试真的可以学到和多东西,也有很多感想. 首先体会最深的就是系主任所说的软件工程不是那么好学的,真的需要 ...

  8. Codeforces 1036E Covered Points (线段覆盖的整点数)【计算几何】

    <题目链接> <转载于 >>>  > 题目大意: 在二维平面上给出n条不共线的线段(线段端点是整数),问这些线段总共覆盖到了多少个整数点. 解题分析: 用GC ...

  9. 给有C或C++基础的Python入门 :Python Crash Course 4 操作列表 4.1--4.3

    操作列表,也就是遍历列表.本章我们要学的就是如何遍历列表. 4.1--4.2 遍历列表 遍历列表,用for循环. 不同于C++或者C语言的for循环,Python的for循环更容易让人理解. 看一个例 ...

  10. BZOJ.5093.[Lydsy1711月赛]图的价值(NTT 斯特林数)

    题目链接 对于单独一个点,我们枚举它的度数(有多少条边)来计算它的贡献:\[\sum_{i=0}^{n-1}i^kC_{n-1}^i2^{\frac{(n-2)(n-1)}{2}}\] 每个点是一样的 ...