当前位置:网站首页>C# Task多线程与队列

C# Task多线程与队列

2020-12-07 16:40:07 程序猿欧文

public static void RunTask(List list, Action action, int threadCount = 5, bool waitFlag = true)
{
ConcurrentQueue queue = new ConcurrentQueue(list);
Task[] tasks = new Task[threadCount];
for (int i = 0; i < threadCount; i++)
{
tasks[i] = Task.Run(() =>
{
T t;
while (queue.TryDequeue(out t))
{
action(t);
}
});
}
if (waitFlag)
{
Task.WaitAll(tasks);
}
}
//调用方法
new System.Threading.Thread(de.........

版权声明
本文为[程序猿欧文]所创,转载请带上原文链接,感谢
https://my.oschina.net/mikeowen/blog/4779166