在c#中创建线程池

发布于 2024-09-27 11:32:40 字数 176 浏览 3 评论 0原文

我有 300 个线程,它们正在一一执行。我使用 join,所以它是一对一的。我想一次执行N个线程。

任何人都可以引导我访问有关在 C# 中创建线程池(具有 N 个线程)的链接。我的情况是一次有N个线程会执行,其余线程会等待。当一个线程执行完毕后,会有一个等待线程进入执行。

任何代码片段都会受到赞赏。多谢。

I have 300 threads which is executing one by one. I use join, so its one-by-one. I want to execute N threads at a time.

Can anyone direct me to a link on creating a thread pool in c# (with N threads). My situation is at a time N threads will execute, the rest of the threads will wait. When one thread finishes execution, one waiting thread will enter into execution.

Any code snippet is appreciated. Thanks a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

A君 2024-10-04 11:32:40

Join 并不规定线程按顺序运行 - 它只是使当前线程等待指定线程完成后再继续。

因此,如果您启动 300 个线程,然后将它们全部连接起来,则这 300 个线程将并行运行,并且一旦 300 个线程完成,连接线程就会完成。

const int COUNT = 300;

// create and start the threads
var threads = new Thread[COUNT];
for (int index = 0; index < COUNT; index += 1)
{
    threads[index] = new Thread(...);
    threads[index].Start();
}

// now they're running, join them all
for (int index = 0; index < COUNT; index += 1)
{
    threads[index].Join();
}

// we're done

重要的是,在开始加入之前启动所有线程,否则您将等待每个线程完成后再开始下一个线程,因此它们实际上是连续的。我想这就是你可能正在做的事情?

Join does not dictate that the threads are run sequentially — it merely makes the current thread wait for the specified thread to finish before continuing.

So if you start 300 threads and then join them all, the 300 threads will run in parallel and the joining thread will complete once the 300 threads are finished.

const int COUNT = 300;

// create and start the threads
var threads = new Thread[COUNT];
for (int index = 0; index < COUNT; index += 1)
{
    threads[index] = new Thread(...);
    threads[index].Start();
}

// now they're running, join them all
for (int index = 0; index < COUNT; index += 1)
{
    threads[index].Join();
}

// we're done

The important part is that you start them all before you start joining, otherwise you will wait for each thread to finish before starting the next, thus then they really would be sequential. I guess this is what you may be doing?

酒与心事 2024-10-04 11:32:40

我想您可能正在寻找 ThreadPool.QueueUserWorkItem。使用线程池减少了线程创建和销毁的大量开销。您可以根据可用的硬件调整线程(链接在我的评论中),或者允许系统最好地管理最小/最大并发线程。如果这是一个 Web 应用程序,您可以在 web.config 中进行设置以设置“N”线程数:

   <system.web>
        <processModel minWorkerThreads="50"/>
   </system.web>

如果您使用的是 .NET 4.0,您还可以使用“Parallel.ForEach”,它将自动安排一个线程的每次迭代。循环到线程池中的多个线程。 (链接在我的评论里)

I think you are probably looking for ThreadPool.QueueUserWorkItem. Using the threadpool reduces the large overhead of thread creation and destruction. You can tune the threads (link in my comments) based on the hardware available, or allow the system to best manage the min/max simultaneous threads. If this is a web app, you can set this in your web.config to set your "N" thread number:

   <system.web>
        <processModel minWorkerThreads="50"/>
   </system.web>

If you are using .NET 4.0 you can also use the "Parallel.ForEach" which will automatically schedule each iteration of a loop onto a multiple threads in the thread pool. (link in my comments)

谈下烟灰 2024-10-04 11:32:40

如果您的大多数线程都在等待,您应该查看 System.Threading.ThreadPool 类。它可能只是做你想做的事。而且它很有效。

If most of your threads are waiting you should have a look at the System.Threading.ThreadPool class. It might just do exactly what you want. And it's efficient.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文