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. C# 之 判断一个字符是否是汉字

    判断一个字符是不是汉字通常有三种方法: [1] 用 ASCII 码判断:[2] 用汉字的 UNICODE 编码范围判断:[3] 用正则表达式判断. 1.用ASCII码判断 在 ASCII码表中,英文的 ...

  2. Codeforces 219E Parking Lot 线段树

    Parking Lot 线段树区间合并一下, 求当前要占的位置, 不包括两端点的写起来方便一点. #include<bits/stdc++.h> #define LL long long ...

  3. JQuery函数大全

    $(”p”).addClass(css中定义的样式类型); 给某个元素添加样式 $(”img”).attr({src:”test.jpg”,alt:”test Image”}); 给某个元素添加属性/ ...

  4. 查看当前的app运行的是哪个Activity

    1.确认手机连接了adb-->检查方式:adb devices 2.手机运行任意app,随意进入一个页面 3.此时cmd运行:adb shell "dumpsys window | g ...

  5. Codeforces 873F Forbidden Indices 字符串 SAM/(SA+单调栈)

    原文链接https://www.cnblogs.com/zhouzhendong/p/9256033.html 题目传送门 - CF873F 题意 给定长度为 $n$ 的字符串 $s$,以及给定这个字 ...

  6. bind注意事项(传引用参数的时候)

    默认情况下,bind的那些不是占位符的参数被拷贝到bind返回的可调用对象中. 当需要把对象传到bind中的参数中时,需要使用ref或者cref. 例如: #include<iostream&g ...

  7. php防攻击

    客户端脚本植入 XSS跨站脚本攻击(跨站脚本攻击,输入(传入)自动执行恶意的HTML代码,如盗取用户Cookie.破坏页面结构.重定向到其它网站):过滤<,>&,"等特殊 ...

  8. TF之RNN:TensorBoard可视化之基于顺序的RNN回归案例实现蓝色正弦虚线预测红色余弦实线—Jason niu

    import tensorflow as tf import numpy as np import matplotlib.pyplot as plt BATCH_START = 0 TIME_STEP ...

  9. HDU1029 Ignatius and the Princess IV (水题)

    <题目链接> 题目大意:给你一段序列,问你在这个序列中出现次数至少为 (n+1)/2 的数是哪个. 解题分析: 本题是一道水题,如果用map来做的话,就非常简单,但是另一个做法还比较巧妙. ...

  10. Effective Java 第三版——59. 熟悉并使用Java类库

    Tips 书中的源代码地址:https://github.com/jbloch/effective-java-3e-source-code 注意,书中的有些代码里方法是基于Java 9 API中的,所 ...