简体   繁体   English

ThreadPool线程需要等待

[英]ThreadPool Thread needs to wait

I have a application which loads of messages need to be handled (eg: 2000 per second). 我有一个应用程序,需要处理大量的消息(例如:每秒2000)。 And the business requirement requires me not to handle the message immediately , but to wait for 2 seconds to handle every single message. 业务需求要求我不要立即处理消息,而是等待2秒钟来处理每条消息。 what I'm doing now is to spin off a thread from thread pool via Task.Factory.StartNew for every message, and do the "waiting 2 seconds" job inside the thread from the pool. 我现在正在做的是通过Task.Factory.StartNew为每个消息从线程池中分离一个线程,并在池中的线​​程内执行“等待2秒”作业。 The problem is when the message load is really high, I always get OutOfMemory exception , although the memory consumption is not actually that high according to Task Manager from Windows OS. 问题是当消息加载真的很高时,我总是得到OutOfMemory异常,尽管根据Windows操作系统的任务管理器,内存消耗实际上并不高。 However if I don't wait inside the thread, then everything is ok. 但是如果我不在线程内等待,那么一切都还可以。

My guess is that when the message load is high, the thread pool threads are not enough to handle all the messages. 我的猜测是,当消息加载很高时,线程池线程不足以处理所有消息。 thus more and more messages are queued to wait to be handled, when the queue size gets really big, it results in a OOM exception . 因此,越来越多的消息排队等待处理,当队列大小变得非常大时,会导致OOM异常。 Have already tried ThreadPool.SetMaxThreads and ThreadPool.SetMinThreads to very high number, but still doesn't work. 已经尝试过ThreadPool.SetMaxThreads和ThreadPool.SetMinThreads到很高的数字,但仍然无法正常工作。 Any advices? 有什么建议吗?

The code looks like this : 代码如下所示:

 ThreadPool.SetMaxThreads(32768, 32768);
 ThreadPool.SetMinThreads(2500, 2500);

public void HanldeMessage(string message)
{
     Task.Factory.StartNew(() => DoWork(message))
}

public void DoWork(string message)
{
   Thread.Sleep(2000);
   // do some work to message
}

Try this. 尝试这个。 You'll need .NET 4.5. 你需要.NET 4.5。

private static void HandleMessage(string message)
{
    DoWork(message);
}

private static async Task DoWork(string message)
{
    await Task.Delay(2000); // instead of thread.Sleep

    // do some work...
    Console.WriteLine(message);
}

Instead of blocking on thread.sleep, this should effectively return the thread to the pool for 2 seconds, and return execution afterward. 这应该有效地将线程返回池中2秒,而不是在thread.sleep上阻塞,然后返回执行。

I recommend you do not block threads, especially when dealing with a scenario where thousands may be needed. 我建议你不要阻止线程,特别是在处理可能需要数千个的场景时。 I'd also recommend you not set ThreadPool.MaxThreads/MinThreads. 我还建议你不要设置ThreadPool.MaxThreads / MinThreads。 You are putting it through a lot of abuse here. 你在这里经历了很多滥用。

Each thread takes up 1MB of stack space. 每个线程占用1MB的堆栈空间。 I'd wager the reason you see the OOM exceptions is because you are creating too many threads, not because of queued requests to make threads. 我打赌你看到OOM异常的原因是因为你创建了太多的线程,而不是因为排队的请求来创建线程。 Your situation above is awful, because after going through all the work to create a thread, it just sits and rots for 2 seconds. 你上面的情况很可怕,因为经过所有的工作来创建一个线程,它只是坐下来腐烂2秒钟。 More work is requested, and the thread-pool does its darnedest to help out by creating another. 需要做更多的工作,并且线程池最难通过创建另一个来帮助解决问题。 With the amount of requests you expect, its only a matter of time til memory becomes the barrier. 随着您期望的请求数量,它只是时间问题直到内存成为障碍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM