Q: How many threads does a. NET program start at runtime?
A: At least three.
1. Start the Main thread of CLR and run the Main method
2. Debugger help thread
3. Finalizer thread
Class Program
{
Static void Main (string [] args)
{
Console. WriteLine ("Main thread: {0 }",
Thread. CurrentThread. ManagedThreadId );
Console. ReadKey ();
}
}
Generally, CLR starts more special threads as needed.
• Finalizer thread: This thread runs GC for garbage collection.
• Concurrent GC threads: GC starts more threads for garbage collection based on the situation.
• Server GC thread: In server GC mode, CLR may create GC hosting heap and recycle threads for each core of a multi-core machine.
• Debugger help thread: This thread is responsible for providing help for debuggers like WinDbg.
• AppDomain uninstall thread: CLR may start a working thread to uninstall the application domain.
• ThreadPool thread: ThreadPool creates a thread based on the situation.