Maximum Number of Threads in a Program

This section provides a tutorial example to try to find out how many threads can be launched in a single application.

From previous section, I learned how to assign CPUs to an application. Now I want to write a C# program test maximum number of threads that can be executed on my computer with 1 CPU.

Here is the test program I wrote:

// CrashThread.cs
// Copyright (c) 2015, HerongYang.com, All Rights Reserved.

using System;
using System.Threading;
public class CrashThread {
   public static void Main() {
      Console.Write("Press ENTER to continue:");
      Console.ReadLine();

      int count = 1000;
      Thread t; 
      for (int n=1; n<=count; n++) {
         t = new Thread(Run);
         t.Start();
         Console.WriteLine(System.DateTime.Now.TimeOfDay
            +": Launched thread "+n);
      }
   }
   public static void Run() {
      while(true);
   }
}

I compiled CrashThread.cs with .NET 4 and started the executable, CrashThread.exe. The program prompt message shows up:

C:\herong>CrashThread.exe

Press ENTER to continue:

Before pressing the "Enter" key, I used Task Manager to change Processor Affinity on CrashThread.exe to use only 1 CPU.

Then I pressed the "Enter" key to see many threads can be added before the program crashes on 1 CPUs.

After 20 minutes, I checked the output:

20:40:18.0832500: Launched thread 1
20:40:18.4582500: Launched thread 2
20:40:19.0363750: Launched thread 3
20:40:19.7863750: Launched thread 4
20:40:20.7551250: Launched thread 5
20:40:21.8957500: Launched thread 6
20:40:22.3957500: Launched thread 7
20:40:22.8957500: Launched thread 8
20:40:23.4582500: Launched thread 9
20:40:24.0832500: Launched thread 10
...
20:41:41.4738750: Launched thread 51
20:41:44.7395000: Launched thread 52
20:41:48.0520000: Launched thread 53
20:41:51.4426250: Launched thread 54
20:41:54.8801250: Launched thread 55
20:41:54.8801250: Launched thread 55
20:41:58.3801250: Launched thread 56
20:42:01.9426250: Launched thread 57
20:42:05.5988750: Launched thread 58
20:42:09.2863750: Launched thread 59
20:42:13.0988750: Launched thread 60
...
20:45:41.9113750: Launched thread 101
20:45:47.9113750: Launched thread 102
20:45:53.9113750: Launched thread 103
20:45:59.9113750: Launched thread 104
20:46:06.4895000: Launched thread 105
20:46:12.9113750: Launched thread 106
20:46:16.2707500: Launched thread 107
20:46:19.6457500: Launched thread 108
20:46:26.5051250: Launched thread 109
20:46:33.3801250: Launched thread 110
...
20:58:49.2863750: Launched thread 151
20:58:56.5988750: Launched thread 152
20:59:10.8332500: Launched thread 153
20:59:25.0988750: Launched thread 154
20:59:39.7863750: Launched thread 155
20:59:54.3488750: Launched thread 156
21:00:08.9113750: Launched thread 157
21:00:24.2238750: Launched thread 158
21:00:39.2238750: Launched thread 159
20:58:35.2863750: Launched thread 150

I was happy to see that 150 threads were launched and the performance of my computer system is still normal. Here were some performance numbers reported on the Task Manager on CrashThread.exe:

CPU:            50%
Mem Usage:   1,612K
VM Size:   177,336K

Conclusion: .NET CLR (Common Language Runtime) handles multiple threads very well. I did not wait long enough to see what will happen at the end.

Last update: 2015.

Table of Contents

 About This Book

 Introduction of C# (C Sharp)

 Data Type and Variables

 Logical Expressions and Conditional Statements

 Arrays and Loop Statements

 Data Type Features

 Floating-Point Data Types

 Passing Parameters to Methods

 Execution Environment Class

 Visual C# 2010 Express Edition

 Class Features

 C# Compiler and Intermediate Language

 Compiling C# Source Code Files

 MSBuild - Microsoft Build Engine

 Memory Usages of Processes

Multithreading in C#

 What Is Multithreading?

 System.Threading.Thread Class

 Creating and Running Threads

 Threads to Run Instance Methods

 Performance Impact with Multiple Threads

 Multi-Thread Programs on Multi-CPU Systems

Maximum Number of Threads in a Program

 Async Feature from C# 5

 System.IO.FileInfo Class

 System.Diagnostics.FileVersionInfo Class

 WPF - Windows Presentation Foundation

 Partial Classes and Partial Methods

 Outdated Tutorials

 References

 PDF Printing Version