.NET程序默认启动线程数

本文探讨了.NET程序在运行时默认启动的线程数量及其类型,包括主线程、调试器帮助线程及Finalizer线程等,并通过示例代码和调试方式验证这些线程的存在。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问:一个.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

转载于:https://www.cnblogs.com/hellolong/p/5818110.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值