当前位置:网站首页>C task multithreading and queue

C task multithreading and queue

2020-12-07 16:40:07 Irving the procedural ape

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);
}
}
// Calling method
new System.Threading.Thread(de.........

版权声明
本文为[Irving the procedural ape]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/202012071637464281.html