Does the number of busy worker threads in the CLR ThreadPool affect performance of I/O threads?
We have a Windows Service which hosts a number of WCF services and, in an unrelated part of the app, makes extensive use of the TPL Task
class to asynchronously do relatively short bits of work.
It is my understanding that WCF uses managed I/O threads from the ThreadPool to execute requests. I noticed that after deploying a feature which significantly raised the applications use of Tasks
, and as such the use of ThreadPool worker threads as开发者_如何学JAVA well, performance of a couple of web services has become very slow. We're talking minutes instead of less than a second. The number of Tasks
actually trying to run at any one time can range between 20 and 1000, which makes me think that any new (last in) work needing some CPU time could be forced to wait for quite some time.
Does the (in my case extremely large) number of busy ThreadPool worker threads affect the ThreadPool's managed I/O threads? Could these two be connected in any way? Or should I start looking elsewhere...
Thanks!
EDIT: I just did a spot check on a live deployment of the app while trying to call one of the long running web services which is quite typical: 70 Tasks running, Windows Service process has about 150 threads, average CPU usage 30-60%. By typical I mean that CPU usage is generally around 40% and rarely exceeds 80%, however there are a large number of Tasks running and threads used by the process.
If your tasks spend any amount of time sleeping or waiting for I/O, you might benefit from starting your tasks with TaskCreationOptions.LongRunning. This causes the thread pool to create more threads, rather than waiting for queued threads to complete.
task = Task.Factory.StartNew(() =>
{
DoLongRunningWork();
}, TaskCreationOptions.LongRunning);
If you want to test the concept, there is an experiment here which you might find helpful.
Note: I've never answered my own question before so i hope this is appropriate. Feel free to comment or disagree, but it is looking like this isn't getting answered by anyone else.
So, the problem causing our performance issues was something completely unrelated and is now solved.
After this was resolved, the web services began responding in a timely manner again. This leads me to conclude that you can have quite a lot of TPL Tasks running concurrently (like the hundreds in my example) and, even with their heavy usage of Thread Pool worker threads, WCF will still be able to use the Thread Pool's managed I/O threads efficiently to handle your web service requests.
精彩评论