A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

C# program to create a thread pool
Q:

C# program to create a thread pool

0

C# program to create a thread pool

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

/*
 * 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));
        }
    }
}

Output:

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 . . .

Explanation:

In the above program, we created a class MyThreadPool that contains two methods ThreadFun1 and ThreadFun2 to represent tasks. The MyThreadPool class also contains a Main() method to start program execution. Here we created Object TP of MyThreadPool class.

for (int i = 0; i < 2; i++)
{
    ThreadPool.QueueUserWorkItem(new WaitCallback(TP.ThreadFun1));
    ThreadPool.QueueUserWorkItem(new WaitCallback(TP.ThreadFun2));
}

The above code, The WaitCallBack() method is used to point the method to execute thread from the pool of thread.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now