网摘 |  收藏 | 

.NET程序默认启动线程数

问:一个.NET程序在运行时到底启动了多少个线程?

答:至少3个。

  1. 启动CLR并运行Main方法的主线程
  2. 调试器帮助线程
  3. Finalizer线程
复制代码
  class Program
  {
    static void Main(string[] args)
    {
      Console.WriteLine("Main thread: {0}",
        Thread.CurrentThread.ManagedThreadId);
      Console.ReadKey();
    }
  }

ProcessExplorer
http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx)

通常,CLR会根据情况启动更多的特殊线程。

  • Finalizer线程:该线程负责运行GC进行垃圾对象回收。
  • 并发的GC线程:GC会根据情况启动更多的线程并发进行垃圾回收。
  • 服务器GC线程:在服务器GC模式下,CLR可能会为多核机器的每个核创建GC托管堆和回收线程。
  • 调试器帮助线程:该线程负责为类似WinDbg等调试器提供帮助。
  • AppDomain卸载线程:CLR可能会启动一个工作线程来卸载应用程序域。
  • ThreadPool线程:ThreadPool会根据情况创建线程。

 

Short answer, there are other threads with the main thread that are required to support the application.

This can be tested in Visual Studio.

Create a simple Console application. Put a debug point, and start debugging. Once the application gets to that debug point, open "Threads" windows by

Debug -> Windows -> Threads

You will see something like:

enter image description here

Some of the above threads are for debugger, but mostly an app would have main thread, GC (Garbage Collection) and Finalizer thread etc.

 

参考:

http://www.cnblogs.com/gaochundong/archive/2013/04/18/dotnet_app_default_thread_count.html

http://stackoverflow.com/questions/38854906/default-threading-model-for-a-net-application

Things to ignore when debugging an ASP.NET hang

posted @ 2016-08-29 15:16  xulonghua219  阅读(670)  评论(0编辑  收藏  举报