返回

c#-Parallel.ForEach 不断产生新线程

发布时间:2022-08-04 11:31:07 336
# flask

当我Parallel.ForEach在我的程序中使用时,我发现有些线程似乎永远不会完成。事实上,它一遍又一遍地产生新线程,这是我没有预料到也绝对不想要的行为。

我能够使用以下代码重现此行为,就像我的“真实”程序一样,都大量使用处理器和内存(.NET 4.0 代码):

public class Node
{
    public Node Previous { get; private set; }

    public Node(Node previous)
    {
        Previous = previous;
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        DateTime startMoment = DateTime.Now;
        int concurrentThreads = 0;

        var jobs = Enumerable.Range(0, 2000);
        Parallel.ForEach(jobs, delegate(int jobNr)
        {
            Interlocked.Increment(ref concurrentThreads);

            int heavyness = jobNr % 9;

            //Give the processor and the garbage collector something to do...
            List nodes = new List();
            Node current = null;
            for (int y = 0; y < 1024 * 1024 * heavyness; y++)
            {
                current = new Node(current);
                nodes.Add(current);
            }

            TimeSpan elapsed = DateTime.Now - startMoment;
            int threadsRemaining = Interlocked.Decrement(ref concurrentThreads);
            Console.WriteLine("[{0:mm\\:ss}] Job {1,4} complete. {2} threads remaining.",
                elapsed, jobNr, threadsRemaining);
        });
    }
}

当在我的四核上运行时,它最初以4个并发线程开始,正如您所期望的那样。然而,随着时间的推移,创建的线程越来越多。最后,这个程序抛出一个OutOfMemoryException:

[00:00] Job    0 complete. 3 threads remaining.
[00:01] Job    1 complete. 4 threads remaining.
[00:01] Job    2 complete. 4 threads remaining.
[00:02] Job    3 complete. 4 threads remaining.
[00:05] Job    9 complete. 5 threads remaining.
[00:05] Job    4 complete. 5 threads remaining.
[00:05] Job    5 complete. 5 threads remaining.
[00:05] Job   10 complete. 5 threads remaining.
[00:08] Job   11 complete. 5 threads remaining.
[00:08] Job    6 complete. 5 threads remaining.
...
[00:55] Job   67 complete. 7 threads remaining.
[00:56] Job   81 complete. 8 threads remaining.
...
[01:54] Job  107 complete. 11 threads remaining.
[02:00] Job  121 complete. 12 threads remaining.
..
[02:55] Job  115 complete. 19 threads remaining.
[03:02] Job  166 complete. 21 threads remaining.
...
[03:41] Job  113 complete. 28 threads remaining.

上面实验的内存使用图如下:

(屏幕截图是荷兰语;上半部分表示处理器使用情况,下半部分表示内存使用情况。)如您所见,几乎每次垃圾收集器妨碍时都会生成一个新线程(可以看出在内存使用量下降的情况下)。

谁能解释为什么会发生这种情况,以及我能做些什么?我只想让 .NET 停止产生新线程,并首先完成现有线程......

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(1)
按点赞数排序
用户头像