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的更多相关文章

  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. 强大的xargs

    xargs 命令是最重要的 Linux 命令行技巧之一.你可以使用这个命令将命令的输出作为参数传递给另一个命令.例如,搜索 png 文件然后对其进行压缩或者其它操作: find. -name *.pn ...

  2. python---初始sqlite3

    ***sqllite不需要单独安装,python2.5以上自带的! ***官方中文文档:https://docs.python.org/2/library/sqlite3.html ***SQLite ...

  3. Flask--第三个例子,写一个接口,该接口返回html前端页面,模板的使用

     将接口数据返回至html前端页面有两种方法 方法一: 1 @app.route('/index',methods=['get']) 2 def open_index(): 3 page=open(' ...

  4. NN:实现BP神经网络的回归拟合,基于近红外光谱的汽油辛烷值含量预测结果对比—Jason niu

    load spectra_data.mat plot(NIR') title('Near infrared spectrum curve—Jason niu') temp = randperm(siz ...

  5. TF:TF定义两个变量相乘之placeholder先hold类似变量+feed_dict最后外界传入值—Jason niu

    #TF:TF定义两个变量相乘之placeholder先hold类似变量+feed_dict最后外界传入值 import tensorflow as tf input1 = tf.placeholder ...

  6. P4781 【模板】拉格朗日插值

    P4781 [模板]拉格朗日插值 证明 :https://wenku.baidu.com/view/0f88088a172ded630b1cb6b4.html http://www.ebola.pro ...

  7. Windows下更改MySQL 数据库文件存放位置

    更改默认的mysql数据库目录 将 C:\Documents and Settings\All Users\Application Data\MySQL\MySQL Server 5.1\data 改 ...

  8. C# LnkHelper

    using System; using System.Collections.Generic; using System.Text; using Microsoft.Win32; using Syst ...

  9. Vuejs学习笔记(一)

    1)    vue.js 的 主要功能 1.模板渲染 2.模块化 3.扩展功能:路由,Ajax 2)    vue.js 模板渲染之双向绑定示例 message 绑定到了 input 对象,所有在in ...

  10. Python实现的各种机器学习算法

    七种算法包括: 线性回归算法 Logistic 回归算法 感知器 K 最近邻算法 K 均值聚类算法 含单隐层的神经网络 多项式的 Logistic 回归算法 01 线性回归算法 在线性回归中,我们想要 ...