.NET thread-pool threads and CLR worker threads

I will try to answer following questions in this article:

  • How many threads are available in .NET thread pool?
  • How long does the CLR take to create a new Non-thread-pool thread?
  • How long does the CLR take to schedule a task on a thread-pool thread?
  • How long does the CLR take to create a new thread-pool thread when it does not have enough thread available in the pool?

 

How many threads are available in .NET thread pool?

Code:

Output:

 

The min number of workerThreads is 8. It is the number of threads available in the tread pool when you start a program. I will prove it with my testing later in this article.

 

How long does the CLR take to create a new Non-thread pool thread?

Code:

Output:

 

In the test, I simple create 100 CRL worker thread, in each tread, it just sleeps for 5 seconds and increase a counter. When counter values reaches to 100, means all threads completed, then the test stops.

The result shows it took 220 milliseconds to create all the threads, so in average, .NET takes about 2.2 milliseconds to create a worker thread.

The whole test took 5252 milliseconds to complete, as each thread sleeps for 5 seconds, and all thread run in parallel. So the numbers all add up. 

Note: 100 threads is a very small sample size, and how long exactly .NET will take to create a thread will vary on different hardware/test environment. The purpose of this article is just to give you some basic ideas of how expensive some operations are, rather than the accuracy. This will apply for the rest of the article.

 

How long does the CLR take to schedule a task on a thread-pool thread?

Code:

Output

 

Again, similarly test, just use thread-pool threads rather than creating our own threads this time. The result shows now it only takes 0.147 milliseconds for each thread to be scheduled, much faster compare to 2.2 milliseconds in last test. Just as expected, right? So far so good, or maybe, so far so boring…

Now, here comes the interesting part, this is the result for total amount of time to complete the test:

 

What?! It is much longer than the last test! Why?!

To show what’s going on, I have printed the thread ID in each worker thread. Here is what it looks like:

 

The first 8 threads were very quickly printed on the screen, pretty much at same time. Then starts from the 9th, the next 8 threads were pretty slow, each one took about 1 second to show on the screen. Then the most of the rest became very fast again, with just a few slower exceptions.

Here is the explanation: As we have showed in the beginning of this article, the min number of workerThreads is 8, and it must be the number of threads available in the thread pool when a program starts. That explains why the first 8 threads started running very fast. Then as the program requires more threads, and thread pool become empty, it starts to create new threads, so the next 8 threads started very slowly. It took about 0.5-1 seconds to create a thread, after the next 8 threads started which took about 5 seconds, and then the previously scheduled threads began to return back to the pool, starting new jobs became fast again until the thread pool is running out.

To prove this, I changed the min worker thread settings and ran the same test again:

The total time spent is just over 5 seconds now, no more time spent on creating new thread-pool thread.

 

Now we want to find out:

How long does the CLR take to create a new thread-pool thread when it does not have enough thread available in the pool?

Code:

Output

 

 Now, we want to find out the time .NET will take to create new thread-pool thread. So we can’t let the worker thread sleep anymore. I used ManualResetEvent to signal the worker threads to return. Hopefully, ManualResetEvent will take no time to trigger the return. But again, accuracy is not a big concern here.

The result shows it took 951.57 milliseconds to create a thread-pool thread.
Now, here comes a question, why it takes so long to create a thread-pool thread? My previous test has just showed it only takes 2.2 milliseconds to create a CLR worker thread? Why thread pool takes so long?

It turns out that the time it spends not only for creating a thread, rather there is some logic for optimising thread pool. It will make decision to create a new thread or wait for a thread to be available. This is from MSDN:

When a minimum is reached, the thread pool can create additional threads or wait until some tasks complete. Beginning with the .NET Framework 4, the thread pool creates and destroys worker threads in order to optimize throughput, which is defined as the number of tasks that complete per unit of time. Too few threads might not make optimal use of available resources, whereas too many threads could increase resource contention.

As most of the MSDN documents, it is not that clear on what it is trying to say. But I think it can be translated into simple English as

“When .NET thread pool is empty, it will only create a new thread for about every 0.5-1 seconds. “

 

Change machine.config for larger number of minWorkerthread

So for a program, if we know we are going to use a lot of threads from thread-pool, e.g. WCF service hosted by IIS, we better have larger number minWorkerThread. We can do this by changing machine.config. (%windir%\Microsoft.NET\Framework64\[version]\config\machine.config)

Here is an example

 <configuration>
 <system.net>
         <connectionManagement>
             <add address="*" maxconnection="24" />
         </connectionManagement>
     </system.net>
     <system.web>
         <processModel autoConfig="true"
             maxWorkerThreads = "100"
             maxIoThreads = "100"
             minWorkerThreads = "50"
             minIoThreads = "50"
         />
         <httpRuntime 
             minFreeThreads="176" 
             minLocalRequestFreeThreads="152" 
         />
     </system.web>
</configuration>

Note: the thread numbers in the config are per CPU. minWorkerThreads = "50", will let me to have 400 threads in the thread pool as my machine has 8 cores. That also explains the magic default minWorkerThreads value “8” (not Chinese lucky number!).  

 

Here is an article about more details of Tuning IIS - machine.config settings

 

Print | posted on Thursday, August 4, 2016 3:33 PM

Feedback

# re: .NET thread-pool threads and CLR worker threads

Left by ocoanet at 8/10/2016 12:08 AM
Gravatar

The min number of workerThreads is 8. It is the number of threads available in the tread pool when you start a program.


I do not think that the CLR starts unused thread-pool threads. In my view, the value returned by GetMinThreads should be considered as a configuration parameter of the thread creation algorithm. As long as the number of thread-pool threads is below this value, the algorithm will immediately create new threads when work items are queued. Once the number of threads reaches this value, the algorithm will delay thread creation.

# re: .NET thread-pool threads and CLR worker threads

Left by Cathy Jones at 8/10/2016 6:58 AM
Gravatar Now, I can us this information to work on this kind of application and processes. - Mark Zokle

# re: .NET thread-pool threads and CLR worker threads

Left by CFU at 8/11/2016 1:45 PM
Gravatar Good point, ocoanet. I guess what I really mean by "available in the tread pool" is: the thread pool does not have to wait for half second or so to create, so they become available really quick. Yes, you are right, we actually don't know they are just available or created on fly.

# re: .NET thread-pool threads and CLR worker threads

Left by Merlinjohn at 1/31/2017 5:38 AM
Gravatar I was here to check the details on creating new non thread pool thread and the thinks explained here fund to be very helpful for me to understand it. Thanks for sharing useful comments and articles like this on your blog and hope you will definitely share more informative like this again.
Spanish with ease

# Target Coupon

Left by Tiya at 6/5/2017 12:34 AM
Gravatar Your posted article is informative and helpful and its languages is understandable. thank you for giving me a chance for comment.

# Target Coupon

Left by tiya at 6/5/2017 12:40 AM
Gravatar Your all post is full of information and its gives inspiration us. and I want share my experience of online shopping with Target Coupon Codes by this offer I have got more discount.

# re: .NET thread-pool threads and CLR worker threads

Left by rebeccahickshq at 6/22/2017 8:04 PM
Gravatar I don't know who you are. Don't even know your name I wish we could talk but I don't have a number to call. So hold your hand up if you hear me, I've been searching but all that I found Is everywhere that I go, is standing alone in the crowd Maybe you're right here in front of me Am I looking too hard, it's hard to see Oh, give me a sign; I'm starting to wonder if you've lost your way I've been right here waiting patiently, Your lane should be right here next to me I need you tonight, think of you all of the time. thank you
super smash flash 2
bloons tower defense 5

# re: .NET thread-pool threads and CLR worker threads

Left by jazz at 7/10/2017 5:43 PM
Gravatar nice.my world

# re: .NET thread-pool threads and CLR worker threads

Left by hellen jos at 7/10/2017 11:56 PM
Gravatar I don’t know anything about .NET. But interested in learning the code. Here you share some code in .NET and is difficult to understand. Please explain the code portion or share some tutorial of .NET basics so that I can learn. same day dry cleaning

# re: .NET thread-pool threads and CLR worker threads

Left by Saniya at 7/11/2017 5:27 PM
Gravatar I love this post it very nice and interesting post.I really enjoyed reading this article , thanks for sharing this article.
escorts service in Jaipur

# re: .NET thread-pool threads and CLR worker threads

Left by RS at 7/12/2017 5:08 AM
Gravatar This is useful information, and the contrived examples do demonstrate the behaviour. However I feel it leaves out some important conclusions:
- Unless you have some serious justification, creating 100 threads is probably not the solution to your problem.
- you should not use threads that spend most of their time waiting on an asynchronous operations e.g. Thread.Sleep() but equally any other kind of blocking IO operation (reading files, reading network etc)
- if you have 100 tasks to complete, with each one requiring 5 seconds of full processing power of a CPU core, scheduling them to the thread pool with the default settings will probably be the absolute fastest way to complete them all in the shortest elapsed time.

# re: .NET thread-pool threads and CLR worker threads

Left by Cathy Martin at 7/30/2017 5:46 AM
Gravatar It’s okay guys, auto update is not really important for me since I always logged in to my site almost daily. And I always ensure the plugins were updated manually. What if every plugins were updated automatically and one developer made mistake Buy Clenbuterol UK which screw the backend? How do you know which plugin cause that?

# re: .NET thread-pool threads and CLR worker threads

Left by Ammy Jackson at 7/30/2017 6:57 AM
Gravatar I love coding and .NET is my favorite programming language. I still remember when the first time I had coded in the programming language 'C'. I was very happy.

The first software that I was put live was on Godaddy hosting that I had purchased using the Godaddy $1 Hosting Coupon. It was a wonderful experience.

# re: .NET thread-pool threads and CLR worker threads

Left by yahyah at 8/15/2017 8:53 PM
Gravatar Valuable info. Lucky me I found your website by accident. I bookmarked it. This article is genuinely good and I have learned lot of things from it concerning blogging. thanks.
transformice

# router customer support number

Left by router support number at 8/17/2017 7:28 PM
Gravatar I hope it will work if anyone is facing this issue. thank you so much that you shared it with us.
Router Support Number

# re: .NET thread-pool threads and CLR worker threads

Left by chromesupportnumber at 8/17/2017 10:03 PM
Gravatar you shared the useful information for who facing such type of issues that time your information will help them.
Chrome Tech Support

# Dell Customer Service

Left by Dell Support at 8/17/2017 10:23 PM
Gravatar If anyone is facing problem with the Dell products then just click the given link. Our customer support team is available for 24 hours to help the users.

Dell Customer Service

# Canon Customer Support

Left by Canon Support at 8/17/2017 11:24 PM
Gravatar If you want to solve any query related to the Canon printer then You can contact with Canon Customer Support.

# re: .NET thread-pool threads and CLR worker threads

Left by iPhone Support at 8/20/2017 5:58 PM
Gravatar .net is a very typical language to understand. here is a brief discussion that clear the basic things. keep posting.
iphone Customer Service

# re: .NET thread-pool threads and CLR worker threads

Left by Epson Support Number at 8/24/2017 7:27 PM
Gravatar Hey! It's a nice to post on the.Net thread pool, many threads running in the background when the program running. Thanks for sharing an informative post.

# re: .NET thread-pool threads and CLR worker threads

Left by Epson Support Number at 8/24/2017 7:31 PM
Gravatar Hey! It's a nice to post on the.Net thread pool, many threads running in the background when the program running. Thanks for sharing an informative post. Epson Support Number

# thanks your post

Left by io games at 8/27/2017 3:45 PM
Gravatar I have read through some similar topics! However, your post has given me a very special impression, unlike other posts. I hope you continue to have valuable articles like this or more to share with everyone!
io games

# re: .NET thread-pool threads and CLR worker threads

Left by vpsman at 8/28/2017 7:21 AM
Gravatar I read this code and can say, php is far easy to use,though asp.net is what I am trying to learn.

Visit for free Linux vps hosting

# re: .NET thread-pool threads and CLR worker threads

Left by hdpunjab at 8/29/2017 10:07 PM
Gravatar Thanks for your personal marvelous posting!

I quite enjoyed reading it, you happen to be a great author.

I will make sure to bookmark your blog and will often come back in the future.
I want to encourage that you continue your great posts, have a nice weekend!
Latest Punjabi Videos

# re: .NET thread-pool threads and CLR worker threads

Left by irupes at 9/10/2017 8:53 PM
Gravatar Thank you for any other wonderful post. The
place else may anybody get that type of information in such
an ideal way of writing? I’ve a presentation next week, and I am at the look for
such info.
HD Videos
Earn Money

# re: .NET thread-pool threads and CLR worker threads

Left by Alisha at 10/9/2017 12:26 AM
Gravatar I am delighted to read this post and I like to read this kind of informative post. Independent Escorts Service in Delhi

# re: .NET thread-pool threads and CLR worker threads

Left by Rajnesh at 10/13/2017 6:23 PM
Gravatar Welcome to MS office support service. We provide solution regarding MS office support number. Visit us to know more.
ms office product keys support

# link

Left by Austin at 10/22/2017 5:48 PM
Gravatar Thanx yeah i have already checked everything, no alert nothing. please share your
experience.

webpage

# re: .NET thread-pool threads and CLR worker threads

Left by ludo king at 10/23/2017 6:08 PM
Gravatar Very useful info, thanks
ludo king

# re: .NET thread-pool threads and CLR worker threads

Left by lisa at 10/31/2017 7:57 PM
Gravatar Phenomenal Information sharing .. I am to a great degree happy to scrutinize this article .. an obligation of appreciation is ll together to give us encounter info.Fantastic respectable. I respect this post.
Escorts Bangalore

# re: .NET thread-pool threads and CLR worker threads

Left by Kayne at 11/1/2017 5:28 PM
Gravatar It’s awesome to come across a blog every once in a while that isn’t the same out of date rehashed information.
iphone Support number

# chrome support

Left by chrome tech support at 11/5/2017 5:48 PM
Gravatar Welcome to chrome support service. We provide solution regarding chrome support number. Visit us to know more.

# re: .NET thread-pool threads and CLR worker threads

Left by www.gmail.com at 11/16/2017 10:58 PM
Gravatar Thanks For Sharing.
www.gmail.com

# re: .NET thread-pool threads and CLR worker threads

Left by jordan at 11/26/2017 12:04 AM
Gravatar I love this post it very nice and interesting post.I really enjoyed reading this article , thanks for sharing this article. free visa gift code generator

# nasir

Left by Embankment staircase at 12/12/2017 1:31 AM
Gravatar I like to do new activity in crafting and your posted blog is really very Embankment staircase interesting and thank you very much for giving me futuristic ideas.

# thanks your sites!

Left by usps tracking at 1/24/2018 4:10 PM
Gravatar It is easy to understand, detailed and meticulous! I have had a lot of harvest after watching this article from you! I feel it interesting, your post gave me a new perspective! I have read many other articles about the same topic, but your article convinced me! I hope you continue to have high quality articles like this to share with veryone!
usps tracking

# re: .NET thread-pool threads and CLR worker threads

Left by Anne at 2/5/2018 8:08 PM
Gravatar Great article I ever saw. It is my first time I visit here. godaddy $1 hosting coupon The way you create a website very thorough and good.

# re: .NET thread-pool threads and CLR worker threads

Left by Anne at 2/6/2018 9:35 PM
Gravatar This is such a great resource that you are providing
Zaful Promo Code and you give it away for free.

# Thank you for the tips.

Left by AnitMam at 2/19/2018 9:15 PM
Gravatar Cheer! Remarkably contribution. Thankee!

# re: .NET thread-pool threads and CLR worker threads

Left by Anna Shetty at 2/21/2018 3:15 PM
Gravatar Amazing article thanks or sharing... play cool math games

# re: .NET thread-pool threads and CLR worker threads

Left by godaddy $1 ASP .NET web hosting at 2/21/2018 5:08 PM
Gravatar I would like to you thanks for giving me always best technical knowledge with your blog post,.Net is one of the best frameworks opensource for creating any website, godaddy $1 ASP .NET web hosting, Where you can get best and cheapest hosting services for your Asp.net website.

# re: .NET thread-pool threads and CLR worker threads

Left by manabadi at 2/23/2018 5:56 PM
Gravatar Telangana SSC board has announced the 10th result notification for the candidates that who all have appeared 10th class. Now, those students are eagerly waiting for the BSE Telangana 10th Results 2018

manabadi.com

# re: .NET thread-pool threads and CLR worker threads

Left by neha sharma at 3/5/2018 11:19 PM
Gravatar Thanks for sharing this article. I always read such kind of articles and always found something new topic for my knowledge and your website perfect for my need. thanks for sharing this wonderful article.
Vip Amritsar Agency Escorts

# re: .NET thread-pool threads and CLR worker threads

Left by ellascott at 3/15/2018 2:37 PM
Gravatar Thanks for the information you brought to us. They are very interesting and new. Look forward to reading more useful and new articles from you!gmail login

# Delhi Escort

Left by soniya sharma at 3/19/2018 9:38 PM
Gravatar clients get fully satisfied with the services provided by Escort in Delhi.

# Thanks for useful post

Left by peter at 3/22/2018 6:56 PM
Gravatar Thanks for the information you brought to us. They are very interesting and new. Look forward to reading more useful and new articles from you

walmartone login |
walmartone.com

# re: .NET thread-pool threads and CLR worker threads

Left by kakbrus at 4/8/2018 4:51 PM
Gravatar Thank you for this wonderful Article! fnaf

# re: .NET thread-pool threads and CLR worker threads

Left by sobhana123 at 4/10/2018 9:16 PM
Gravatar hi there.. really nice information.. i was looking for this since a long time…really appreciate the amount of work which you have put into to it.. thanks for sharing.
ISO 14001 Certification in qatar

# Bangalore escort

Left by safewalk bangalore at 4/15/2018 8:08 PM
Gravatar Are you looking for Escorts Bangalore or some fun with our Female high profile escort service in Bangalore.
http://bit.ly/2hxpCGM

# escort Bangalore

Left by vipservice at 4/15/2018 8:14 PM
Gravatar VIP Escort Service offers you high profile escort service in Bangalore.

http://bit.ly/2yv1wA3

# re: .NET thread-pool threads and CLR worker threads

Left by velevobowi at 5/18/2018 10:38 PM
Gravatar KFC launched a web portal to collect the customer feedback survey. As the mykfcexperience company believes that the feedback survey helps them to improve their business and reach out to customer expectations and needs.

# Flud For PC

Left by Robin at 5/22/2018 12:39 AM
Gravatar The miniature variation for little bit gush makes it less complicated for you to obtain all the attributes of the little bit gush application right in the hand of your hands.Flud For PC

# re: .NET thread-pool threads and CLR worker threads

Left by RRBExam at 5/30/2018 8:38 AM
Gravatar This website helps me a lot to learn the about.Net language RRBExam

Your comment:





 

Copyright © Changhong Fu

Design by Bartosz Brzezinski

Design by Phil Haack Based On A Design By Bartosz Brzezinski