-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreadPool.cs
40 lines (38 loc) · 1.17 KB
/
threadPool.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/*
* C# Program to Create Thread Pools
*/
using System;
using System.Threading;
class ThreadPoolDemo
{
//Defines our two callback methods.
public void task1(object obj)
{
for (int i = 0; i <= 2; i++)
{
Console.WriteLine("Task 1 is being executed");
}
}
public void task2(object obj)
{
for (int i = 0; i <= 2; i++)
{
Console.WriteLine("Task 2 is being executed");
}
}
static void Main()
{
//ThreadPool is an abstraction that allows for automatic management of
//Available process threads without manually setting thread properties.
ThreadPoolDemo tpd = new ThreadPoolDemo();
for (int i = 0; i < 2; i++)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(tpd.task1));
ThreadPool.QueueUserWorkItem(new WaitCallback(tpd.task2));
//The QueueUserWorkItem method takes a callback function to execute on the ThreadPool.
//The Threads in the the ThreadPool will be assigned these work items.
}
Console.Read();
}
}
//Taken from https://www.sanfoundry.com/csharp-program-create-thread-pools/