-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiThreadExample.cs
More file actions
60 lines (54 loc) · 1.87 KB
/
MultiThreadExample.cs
File metadata and controls
60 lines (54 loc) · 1.87 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ThreadExample
{
public class MultiThreadExample
{
public static void ExecuteThread()
{
//Object of number counter
NumberCounter counter = new();
//First thread start delegate
ThreadStart countUpThread = new(counter.CounterUp);
//Initialize thread
Thread thread1 = new(countUpThread)
{
Name = "Count-up Thread",
Priority = ThreadPriority.Highest
};
//Check the thread state
Console.WriteLine($"{thread1.Name} ({thread1.ManagedThreadId}) is {thread1.ThreadState}");
//Invok countUp method
thread1.Start();
//Check the thread state
Console.WriteLine($"{thread1.Name} is {thread1.ThreadState}");
//Second thread start delegate
//ThreadStart countDownThread = new(counter.CounterDown);
ThreadStart countDownThread = new(() =>
{
counter.CounterDown();
}); //Parameterized thread
//Initialize thread
Thread thread2 = new(countDownThread)
{
Name = "Count-down Thread",
Priority = ThreadPriority.Normal
};
//Check the thread state
Console.WriteLine($"{thread2.Name} ({thread2.ManagedThreadId}) is {thread2.ThreadState}");
//Invok countDown method
thread2.Start();
//Check the thread state
Console.WriteLine($"{thread2.Name} is {thread2.ThreadState}");
//Join
thread1.Join();
thread2.Join();
//Interrupt
//Thread.Sleep(1000);
//thread1.Interrupt();
}
}
}