简体   繁体   中英

Task Scheduler (Task.Factory) and controlling the number of threads

I've been reading up on multithreading to get something more than the regular "push functions to the threadpool and wait for it to finish" approach which is really basic.

Basically, I want more control over the threads, the ability to pass on Cancelation tokens, get return values, etc. This all looks possible with the use of Task.factory (Task Scheduler), which from what i understand runs on top of the threadpool.

If that's the case, if I limit the thread number on the general threadpool, that will apply to my implementation of Task Scheduler or?

I also read that using your own threadpool is better than THE threadpool, can I mix these two up and get the control I want?

Any suggestions are welcome! Thanks for taking the time to explain a bit more guys.

You can create a TaskScheduler that limits concurrency . This custom scheduler can then be used to create your own TaskFactory , and start tasks that are customized with the control you wish.

The Parallel Extensions Samples project includes many custom task schedulers you can use as reference.

I also read that using your own threadpool is better than THE threadpool, can I mix these two up and get the control I want?

I would actually disagree with this, for most general uses. The .NET ThreadPool is very efficient, and highly optimized. It includes quite a few metrics for automaticallly scaling the number of threads used, etc.

That being said, you can always make a TaskScheduler which uses dedicated threads or your own "thread pool" implementation if you choose.

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