title: Threading in .NET core ASP Controllers
body: <p>I've made a clean project of .net Core 3.0 Web application and I am trying to understand, how <code>ThreadPool</code> works in C#.</p>
  
  <pre><code>namespace TestASPSelf.Controllers
  {
      public class HomeController : Controller
      {
          private readonly ILogger&lt;HomeController&gt; _logger;
  
          public static int countThread = 0;
  
          public HomeController(ILogger&lt;HomeController&gt; logger)
          {
              _logger = logger;
          }
  
          public IActionResult Index()
          {
  
              int workerThreads;
              int portThreads;
  
              ThreadPool.GetMaxThreads(out workerThreads, out portThreads);
              Console.WriteLine("\nMaximum worker threads: \t{0}" +
                                "\nMaximum completion port threads: {1}",
                  workerThreads, portThreads);
  
              ThreadPool.GetAvailableThreads(out workerThreads,
                  out portThreads);
              Console.WriteLine("\nAvailable worker threads: \t{0}" +
                                "\nAvailable completion port threads: {1}\n",
                  workerThreads, portThreads);    
  
                  Console.WriteLine("countThread = " + countThread);
  
              return View();
          }
  
          class Z
          {
              public static void WaitTest(object o)
              {
                  countThread++;
                  while (true)
                  {
                      Thread.Sleep(1000);
                  }
              }
          }
  
          public IActionResult Privacy()
          {
              for (int i = 0; i &lt; 100; i++)
              {
                  Console.WriteLine("starting thread "+i);
                  ThreadPool.QueueUserWorkItem(new WaitCallback(Z.WaitTest));
              }
  
              return View();
          }
  }
  }
  </code></pre>
  
  <p>When <a href="http://localhost:5000/Home/Privacy" rel="nofollow noreferrer">http://localhost:5000/Home/Privacy</a> opened, it hangs for some time (for about 40-80 seconds), but I see, that logic of <code>for</code> cycles in it completes almost instantly.
  When <a href="http://localhost:5000/" rel="nofollow noreferrer">http://localhost:5000/</a> opened after that, it hangs for 40-80 seconds too and result is in console <code>countThread = 100</code>.
  CPU usage of app is about 5-10%, when threads were started.</p>
  
  <p>I am trying to understand:</p>
  
  <p>1) The first one is why ASP controller hangs for 40-80 seconds per page, when 100 Threads are running by CPU usage 5-10 percents. CPU has a lot of resources, RAM is free too, but why ASP Controller methods of pages are hangs?</p>
  
  <p>2) How to create ThreadPool in C# with limited count of running threads? If I understand method <code>public static bool SetMinThreads (int workerThreads, int completionPortThreads);</code> correctly, it affects globally all threads of the app. How to create object of ThreadPool with limited count of active threads, like <code>ExecutorService</code> in Java? For example, Java code of thread pool could look like</p>
  
  <pre><code>ExecutorService executor = Executors.newFixedThreadPool(5);
          for (int i = 0; i &lt; 10; i++) {
              Runnable worker = new WorkerThread("" + i);
              executor.execute(worker);
            }
          executor.shutdown();
          while (!executor.isTerminated()) {
          }
  </code></pre>
  
  <p>3) How to prevent hangs of all methods of ASP Controller and to make "truly real" threads, like in Java?</p>