private readonly SemaphoreSlim _throttler = new SemaphoreSlim(16);//允许指定同时可以访问资源的线程数。 public async Task BatchTaskTest<T>(List<T> lst, int batchSize = 100) { var batches = lst.Select((item, index) => new { item, index }) .GroupBy(x => x.index / batchSize) .Select(x => x.Select(f => f.item).ToList()) .ToArray();//经典 foreach (var datas in batches) { var tasks = new Task[datas.Count]; try { for (int i = 0; i < datas.Count; i++) { tasks[i] = PrivateTask(datas[i]); } await Task.WhenAll(tasks); } catch (Exception e) { await Loger.WriteLog("XXXXX", e); } } } private async Task PrivateTask<T>(T item) { await _throttler.WaitAsync(); try { Thread.Sleep(2000);//待执行任务 } finally { _throttler.Release(); } }
标签:index,Task,批量,处理,item,tasks,var,数据,datas From: https://www.cnblogs.com/a976986319/p/18435079