C# Program to Create a Thread Pool

A thread pool in C# is a collection of pre-created threads that can be reused to execute multiple tasks efficiently. Instead of creating new threads for each task, the thread pool manages a pool of worker threads, reducing the overhead of thread creation and destruction.

The ThreadPool class provides methods to queue work items for execution by available threads in the pool. This approach is particularly useful for short-running tasks that don't require dedicated threads.

Syntax

Following is the syntax for queueing a method for execution in the thread pool −

ThreadPool.QueueUserWorkItem(new WaitCallback(methodName));

The method signature for a thread pool work item must match the WaitCallback delegate −

public void MethodName(object state) {
   // method implementation
}

How Thread Pool Works

Thread Pool Architecture Work Queue Task 1 Task 2 Thread 1 Thread 2 Thread N Task Execution Results

Basic Thread Pool Example

Following example demonstrates how to create and use a thread pool with multiple worker methods −

using System;
using System.Threading;

class Demo {
   public void TaskOne(object state) {
      for (int i = 0; i <= 3; i++) {
         Console.WriteLine("Task One executed - " + i);
         Thread.Sleep(100); // Simulate work
      }
   }
   
   public void TaskTwo(object state) {
      for (int i = 0; i <= 3; i++) {
         Console.WriteLine("Task Two executed - " + i);
         Thread.Sleep(100); // Simulate work
      }
   }
   
   public void TaskThree(object state) {
      for (int i = 0; i <= 3; i++) {
         Console.WriteLine("Task Three executed - " + i);
         Thread.Sleep(100); // Simulate work
      }
   }
   
   static void Main() {
      Demo demo = new Demo();
      
      // Queue multiple tasks for execution
      for (int i = 0; i < 2; i++) {
         ThreadPool.QueueUserWorkItem(new WaitCallback(demo.TaskOne));
         ThreadPool.QueueUserWorkItem(new WaitCallback(demo.TaskTwo));
         ThreadPool.QueueUserWorkItem(new WaitCallback(demo.TaskThree));
      }
      
      // Wait for all tasks to complete
      Thread.Sleep(2000);
      Console.WriteLine("All tasks completed.");
   }
}

The output of the above code is −

Task One executed - 0
Task Two executed - 0
Task Three executed - 0
Task One executed - 1
Task Two executed - 1
Task Three executed - 1
Task One executed - 0
Task Two executed - 0
Task Three executed - 0
Task One executed - 1
Task Two executed - 1
Task Three executed - 1
Task One executed - 2
Task Two executed - 2
Task Three executed - 2
Task One executed - 3
Task Two executed - 3
Task Three executed - 3
Task One executed - 2
Task Two executed - 2
Task Three executed - 2
Task One executed - 3
Task Two executed - 3
Task Three executed - 3
All tasks completed.

Using Lambda Expressions with Thread Pool

You can also use lambda expressions to simplify thread pool usage −

using System;
using System.Threading;

class Program {
   static void Main() {
      // Queue tasks using lambda expressions
      for (int taskNum = 1; taskNum <= 3; taskNum++) {
         int currentTask = taskNum; // Capture variable for closure
         ThreadPool.QueueUserWorkItem(state => {
            for (int i = 0; i < 3; i++) {
               Console.WriteLine($"Lambda Task {currentTask} - Step {i}");
               Thread.Sleep(50);
            }
         });
      }
      
      // Wait for completion
      Thread.Sleep(1000);
      Console.WriteLine("Lambda tasks completed.");
   }
}

The output of the above code is −

Lambda Task 1 - Step 0
Lambda Task 2 - Step 0
Lambda Task 3 - Step 0
Lambda Task 1 - Step 1
Lambda Task 2 - Step 1
Lambda Task 3 - Step 1
Lambda Task 1 - Step 2
Lambda Task 2 - Step 2
Lambda Task 3 - Step 2
Lambda tasks completed.

Key Characteristics

  • Automatic thread management: The .NET runtime manages thread creation, reuse, and destruction.

  • Background threads: Thread pool threads are background threads that don't prevent application termination.

  • No guaranteed execution order: Tasks may execute in any order depending on thread availability.

  • Suitable for short tasks: Best for CPU-bound tasks that complete quickly.

Conclusion

Thread pools in C# provide an efficient way to execute multiple short-running tasks concurrently without the overhead of creating individual threads. Use ThreadPool.QueueUserWorkItem() to queue methods for execution by available worker threads in the pool.

Updated on: 2026-03-17T07:04:35+05:30

744 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements