C# Program to Create a Thread Pool

You are currently viewing C# Program to Create a Thread Pool

C# program to create a thread pool

/*
 * Program to Create Thread Pools in C#
 */
using System;
using System.Threading;

class MyThreadPool
{
    public void ThreadFun1(object obj)
    {
        int loop = 0;
        for (loop = 0; loop <= 4; loop++)
        {
            Console.WriteLine("Thread1 is execting");
        }
    }
    public void ThreadFun2(object obj)
    {
        int loop = 0;
        for (loop = 0; loop <= 4; loop++)
        {
            Console.WriteLine("Thread2 is execting");
        }
    }

    static void Main()
    {
        MyThreadPool TP = new MyThreadPool();

        for (int i = 0; i < 2; i++)
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(TP.ThreadFun1));
            ThreadPool.QueueUserWorkItem(new WaitCallback(TP.ThreadFun2));
        }
    }
}
Thread1 is execting
Thread1 is execting
Thread1 is execting
Thread1 is execting
Thread1 is execting
Thread2 is execting
Thread2 is execting
Thread2 is execting
Thread2 is execting
Thread2 is execting
Thread1 is execting
Thread1 is execting
Thread1 is execting
Thread1 is execting
Thread1 is execting
Thread2 is execting
Thread2 is execting
Thread2 is execting
Thread2 is execting
Thread2 is execting
Press any key to continue . . .

Ranjith Kumar

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply