Skip to main content
 首页 » 编程设计

c#之使用 C# 的线程池问题

2024年08月06日39虾米哥

我正在做我的大学项目。主要要求之一是使用多线程(用户可以选择线程数)。

我是 C# 新手,基于互联网研究。我选择 ThreadPool。

我花了很多时间观察线程如何使用 VS 中的并行监视进行操作,但我不知道它是如何工作的。例如 threadNumber = 10 但并行观察只显示 4 个激活的线程。

这是我的代码:

public void calculateBeta() 
    { 
        var finished = new CountdownEvent(1); 
        for (int i = 0; i < threadNumber; i++) 
        { 
            finished.AddCount(); 
            ThreadPool.QueueUserWorkItem( 
            (state) => 
            { 
                try 
                { 
                   doSth(); 
                } 
                finally 
                { 
                    finished.Signal(); 
                } 
            }); 
        } 
        finished.Signal(); 
        finished.Wait(); 
    } 

我做错了什么?我尝试使用许多不同的线程数值来测试此代码,但它没有像我寻找的那样工作。

编辑:

 private void myTask(object index) 
    { 
 
            int z = (int)index; 
            double[] result = countBeta(createTableB(z), createTableDiagonalA(z)); 
            int counter = 0; 
            if ((rest != 0) && (z == threadNumber - 1)) 
            { 
                for (int j = z * numbersInRow; j < (z + 1) * numbersInRow + rest; j++) 
                { 
                    N[j] = result[counter]; 
                    counter++; 
                } 
            } 
            else 
            { 
                for (int j = z * numbersInRow; j < (z + 1) * numbersInRow; j++) 
                { 
                    N[j] = result[counter]; 
                    counter++; 
                } 
            } 
            threads[z] = true; 
    } 
 
 
public void calculateBeta() 
    { 
        N = new double[num]; 
        setThreadNumber(2); 
        checkThreadNumber(); 
        setNumberInRow(); 
        setRest(); 
        threads = new bool[threadNumber]; 
        for (int i = 0; i < threadNumber; i++) 
        { 
            Thread thread = new Thread(this.myTask); 
            thread.IsBackground = true; 
            thread.Start(i); 
        } 
        while (!checkThreads()) 
        { 
        } 
    } 
 
private bool checkThread() 
    { 
        bool result = true; 
        for (int i = 0; i < threads.Length; i++) 
        { 
            if (!threads[i]) 
                result = false; 
        } 
 
        return result; 
    } 
 
static void Main(string[] args) 
    { 
 
 
        Jacobi jacobi = new Jacobi(); 
        Console.WriteLine("Metoda Jacobiego"); 
        Console.WriteLine("Rozwiazywanie ukladu n-rownan z n-niewiadomymi Ax=b"); 
        jacobi.getNum(); 
        jacobi.getA(); 
        jacobi.getB(); 
        jacobi.calculateBeta(); 
        jacobi.calculateM(); 
        jacobi.calculateX(); 
        jacobi.countNorms(); 
        Console.ReadLine(); 
    } 

我需要 calculateBeta 的结果来进一步计算。有时线程还没有完成,但程序在没有需要线程提供的数据的情况下继续前进。我现在正在使用 bool 变量,但这个解决方案不是处理它的优雅方法(创建 bool 表,检查是否所有线程都已完成)我如何以不同的方式管理它?

请您参考如下方法:

这是因为您正在使用 ThreadPool 来管理您的线程。它会根据很多因素创建一定数量的线程。您可以调整一些设置,但总的来说,当您 promise 使用 ThreadPool 来管理您提交给黑盒的线程时。查看GetMaxThreads和 GetMinThreads以及他们的 setter 对应物,了解您的一些选择。

看看这个 ThreadPool Architecture article在 MSDN 上。它为类(class)的方式和原因提供了良好的背景。但在介绍性段落中,您会看到这句话,这是解决您难题的关键:

The thread pool is primarily used to reduce the number of application threads and provide management of the worker threads.

如果你想拥有快速连续启动 10 个线程的那种控制,你应该避免使用 ThreadPool 并自己管理线程。这是一个启动十个线程并将不同数据传递给每个线程的简单、绝对最小的示例,在本例中为索引:

void ButtonClickHandlerOrSomeOtherMethod()   
{ 
    for (int i=1; i<=10; i++) // using a 1-based index 
    { 
        new Thread(ThreadTask).Start(i); 
    } 
} 
 
void ThreadTask(object i)  
{ 
    Console.WriteLine("Thread " + i + " ID: " + Thread.CurrentThread.ManagedThreadId); 
} 

和一些示例输出:

Thread 1 ID: 19 
Thread 2 ID: 34 
Thread 3 ID: 26 
Thread 4 ID: 5 
Thread 5 ID: 36 
Thread 6 ID: 18 
Thread 7 ID: 9 
Thread 8 ID: 38 
Thread 9 ID: 39 
Thread 10 ID: 40 

Follow-up code demonstrating synching with threads and "waiting" until they are all finished:

void ButtonClickHandlerOrSomeOtherMethod()  
{ 
    // need a collection of threads to call Join after Start(s) 
    var threads = new List<Thread>(); 
 
    // create threads, add to List and start them 
    for (int i=1; i<=10; i++) { 
        var thread = new Thread(ThreadTask); 
        threads.Add(thread); 
        // a background thread will allow main app to exit even 
        // if the thread is still running 
        thread.IsBackground = true; 
        thread.Start(i); 
    } 
 
    // call Join on each thread which makes this thread wait on 
    // all 10 other threads 
    foreach (var thread in threads)  
        thread.Join(); 
 
    // this message will not show until all threads are finished 
    Console.WriteLine("All threads finished."); 
} 
 
void ThreadTask(object i)  
{ 
    Console.WriteLine("Thread " + i + " ID: " + Thread.CurrentThread.ManagedThreadId); 
    // introducing some randomness to how long a task "works on something" 
    Thread.Sleep(100 * new Random().Next(0, 10)); 
    Console.WriteLine("Thread " + i + " finished."); 
}