I am using the TPL to add new tasks to the system thread pool using the function Task.Factory.StartNew()
. The only problem is that I am adding a lot of threads and I think it is creating too many for my processor to handle. Is there a way to set a maximum number of threads in this thread pool?
相关问题
- How to let a thread communicate with another activ
- Why it isn't advised to call the release() met
- ThreadPoolTaskScheduler behaviour when pool is ful
- Custom TaskScheduler, SynchronizationContext?
- How do I create a multidimensional array of object
相关文章
- Difference between Thread#run and Thread#wakeup?
- Java/Spring MVC: provide request context to child
- Threading in C# , value types and reference types
- RMI Threads prevent JVM from exiting after main()
- Efficient signaling Tasks for TPL completions on f
- Async task does not work properly (doInBackground
- Android, Volley Request, the response is blocking
- parallelizing matrix multiplication through thread
Typically TPL determines a good "default" threadpool size. If you really need fewer threads, see How to: Create a Task Scheduler That Limits the Degree of Concurrency
The default
TaskScheduler
(obtained fromTaskScheduler.Default
) is of type (internal class)ThreadPoolTaskScheduler
. This implementation uses theThreadPool
class to queue tasks (if theTask
isn't created withTaskCreationOptions.LongRunning
- in this case a new thread is created for each task).So, if you want to limit the # of threads available to
Task
objects created vianew Task(() => Console.WriteLine("In task"))
, you can limit the available threads in the global threadpool like this:The call to
ThreadPool.GetMaxThreads()
is done to avoid shrinking thecompletionPortThreads
.Note that this may be a bad idea - since all Tasks without a specified scheduler, and any number of other classes use the default ThreadPool, setting the size too low could cause side-effects: Starvation, etc.
Usually the TPL scheduler should do a good job of choosing how many tasks to run concurrently, but if you really want to have control over it My blog post shows how to do this both with Tasks and with Actions, and provides a sample project you can download and run to see both in action.
An example of when you might want to explicitly limit how many tasks are ran concurrently is when you are calling your own services and do not want to overload your server.
For what you are describing, it sounds like you may benefit more from making sure you are using async/await with your tasks to prevent needless thread consumption. This will depend though on if you are doing CPU-bound work, or IO-bound work in your tasks. If it's IO-bound, then you may benefit greatly from using async/await.
Regardless, you asked how you can limit the number of tasks that run concurrently, so here's some code to show how to do it with both Actions and Tasks.
With Actions
If using Actions, you can use the built-in .Net Parallel.Invoke function. Here we limit it to running at most 3 threads in parallel.
With Tasks
Since you are using Tasks here though, there is no built-in function. However, you can use the one that I provide on my blog.
And then creating your list of Tasks and calling the function to have them run, with say a maximum of 3 simultaneous at a time, you could do this:
You should investigate your performance problems first. There are various issues that may result in reduced utilization:
In any case you have a scalability issue that can't be addressed simply by reducing the number of concurrent tasks. Your program may run in a two-,four-, or eight-core machine in the future. Limiting the number of scheduled tasks will simply lead to waste of CPU resources.