简体   繁体   中英

How do I create a fixed-size ThreadPool in .NET?

我想在.NET中创建一个固定的任意大小的ThreadPool - 我知道默认大小是25 - 但我希望有不同的大小,例如5或10.任何人?

You should be careful about changing the size of the thread pool. There is just one fixed system thread pool, used by all kinds of things. Making it too small could cause problems in areas you didn't even think you were using.

If you want to have a relatively small thread pool for one specific task, you should use a separate pool. There are various third party pools available - I have a rather old one as part of MiscUtil , but it should be good enough for simple use cases. I'm sure you can find more advanced ones if you look.

It's unfortunate that there isn't an instantiable ThreadPool in the framework yet. I can't remember offhand whether Parallel Extensions will effectively provide one, but I don't think it will.

ThreadPool.SetMaxThreads()

You can use ThreadPool.SetMinThreads and ThreadPool.SetMaxThreads to have some control over the number of threads in the thread pool.

That being said, I recommend being cautious in using this. It's easy to get yourself into trouble, as many operations in the BCL rely on threadpool threads being available.

ThreadPool.SetMaxThreads(5,5)然后超过五个线程的任何内容将排队。

您需要ThreadPool.SetMaxThreads()方法。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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