c# - 如何查看 Parallel.ForEach 循环中引发的线程数?

标签 c# multithreading parallel-processing

给定这样的场景,在低功率机器上运行:

var logFile = File.ReadAllLines("logpath");
var logList = new List<string>(logFile);

Parallel.ForEach(logList.Batch(8), fileLine =>
{
    // work on the 8 lines
});

有没有一种简单的方法可以找出在 Parallel.ForEach 进程中产生的最大并发线程数?

最佳答案

使用 Console.WriteLine 或 Logging 跟踪循环中的每个线程。

Parallel.ForEach(logList.Batch(8), fileLine =>
{

    // work on the 8 lines
    Console.WriteLine("Processing {0} on thread {1}", filename, Thread.CurrentThread.ManagedThreadId);

});

关于c# - 如何查看 Parallel.ForEach 循环中引发的线程数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45610479/

相关文章:

java - Blackberry java,使用同步从非 UI 线程打开对话框

c - 在 C 中使用 MPI 出现段错误?

ruby - 如何设计一个多线程程序,其中每个线程同时读取来自控制台的输入?

c# - 在 C# 中将 RichTextFormat 信息转换为 HTML 的最佳解决方案是什么?

java - 是什么导致了 java.lang.IllegalMonitorStateException

c# - FindAll 具有不同相关性的条件

c++ - 如何多次发送SIGINT信号

concurrency - 将 "insertions"并行化为 Haskell 中的二进制 trie

c# - 如何在 C# 中使用 opengl 捕获桌面屏幕截图?

c# - 序列化和反序列化 AjaxFileUploadEventArgs - 未定义无参数构造函数'