C#用于循环的TPL-限制线程数

C#用于循环的TPL-限制线程数,c#,multithreading,task-parallel-library,C#,Multithreading,Task Parallel Library,我只想使用2个线程来创建for循环。我尝试了以下代码: ParallelOptions po = new ParallelOptions { MaxDegreeOfParallelism = 2 }; Parallel.For(0, width, po, x => { sb.Append(Thread.CurrentThread.ManagedThreadId); sb.Append(Environment.NewLine); for (int y = 0

我只想使用2个线程来创建for循环。我尝试了以下代码:

ParallelOptions po = new ParallelOptions {
    MaxDegreeOfParallelism = 2
};

Parallel.For(0, width, po, x => {
    sb.Append(Thread.CurrentThread.ManagedThreadId);
    sb.Append(Environment.NewLine);
    for (int y = 0; y < height; y++) {
        double a = (double)(x - (width / 2)) / (double)(width / 4);
        double b = (double)(y - (height / 2)) / (double)(height / 4);
    }
});

但它也不会改变任何事情。有人可能知道如何解决这个问题?

最大并行度设置了将用于
并行.for()的最大并发线程数。这并不意味着只会使用两个线程

在执行
Parallel.For()
期间,可以从线程池中分配不同的线程,因为线程池线程是专门为重用而设计的

下面的程序演示。如果您运行它,您将看到正在使用的不同线程的总数可以超过2个,但同时使用的线程总数永远不会超过2个

using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main()
        {
            ParallelOptions po = new ParallelOptions
            {
                MaxDegreeOfParallelism = 2
            };

            var activeThreads = new ConcurrentDictionary<int, bool>();

            Parallel.For(0, 100, po, x =>
            {
                activeThreads[Thread.CurrentThread.ManagedThreadId] = true;
                Console.WriteLine("Active threads: " + string.Join(", ", activeThreads.Keys));
                Thread.Sleep(200);
                activeThreads.TryRemove(Thread.CurrentThread.ManagedThreadId, out bool unused);
            });

            Console.ReadLine();
        }
    }
}
使用系统;
使用System.Collections.Concurrent;
使用系统线程;
使用System.Threading.Tasks;
名称空间控制台EAPP1
{
班级计划
{
静态void Main()
{
ParallelOptions po=新的ParallelOptions
{
MaxDegreeOfParallelism=2
};
var activeThreads=新的ConcurrentDictionary();
平行。对于(0,100,po,x=>
{
activeThreads[Thread.CurrentThread.ManagedThreadId]=true;
WriteLine(“活动线程:”+string.Join(“,”,activeThreads.Keys));
睡眠(200);
activeThreads.TryRemove(Thread.CurrentThread.ManagedThreadId,out bool unused);
});
Console.ReadLine();
}
}
}

我希望您不要在多线程环境中使用
StringBuilder
,因为它不是线程安全的。我使用它只是为了生成具有所有线程id的大字符串。您是否认为,该StringBuilder在运行两个以上线程时会出现问题?是的。只需使用
Console.WriteLine(Environment.CurrentManagedThreadId)
Trace.TraceInformation…..
仍然不起作用:/@MarMosh这不是答案
using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main()
        {
            ParallelOptions po = new ParallelOptions
            {
                MaxDegreeOfParallelism = 2
            };

            var activeThreads = new ConcurrentDictionary<int, bool>();

            Parallel.For(0, 100, po, x =>
            {
                activeThreads[Thread.CurrentThread.ManagedThreadId] = true;
                Console.WriteLine("Active threads: " + string.Join(", ", activeThreads.Keys));
                Thread.Sleep(200);
                activeThreads.TryRemove(Thread.CurrentThread.ManagedThreadId, out bool unused);
            });

            Console.ReadLine();
        }
    }
}