-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
86 lines (73 loc) · 2.5 KB
/
Copy pathProgram.cs
File metadata and controls
86 lines (73 loc) · 2.5 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// See https://aka.ms/new-console-template for more information
using TaskSimulation;
Console.WriteLine("Hello, World!");
//ThreadPoolDemo();
//DedicatedThreaDemo();
//ContinueJob();
await Foo();
await Bar();
await Baz();
Console.ReadLine();
void ThreadPoolDemo()
{
_ = Job.Run(() => Console.WriteLine($"job1 is excuted in thead {Thread.CurrentThread.ManagedThreadId}"));
_ = Job.Run(() => Console.WriteLine($"job2 is excuted in thead {Thread.CurrentThread.ManagedThreadId}"));
_ = Job.Run(() => Console.WriteLine($"job3 is excuted in thead {Thread.CurrentThread.ManagedThreadId}"));
_ = Job.Run(() => Console.WriteLine($"job4 is excuted in thead {Thread.CurrentThread.ManagedThreadId}"));
_ = Job.Run(() => Console.WriteLine($"job5 is excuted in thead {Thread.CurrentThread.ManagedThreadId}"));
_ = Job.Run(() => Console.WriteLine($"job6 is excuted in thead {Thread.CurrentThread.ManagedThreadId}"));
}
void DedicatedThreaDemo()
{
JobScheduler.Current = new DedicatedThreadJobScheduler(2);
_ = Job.Run(() => Console.WriteLine($"job11 is excuted in thead {Thread.CurrentThread.ManagedThreadId}"));
_ = Job.Run(() => Console.WriteLine($"job12 is excuted in thead {Thread.CurrentThread.ManagedThreadId}"));
_ = Job.Run(() => Console.WriteLine($"job13 is excuted in thead {Thread.CurrentThread.ManagedThreadId}"));
_ = Job.Run(() => Console.WriteLine($"job14 is excuted in thead {Thread.CurrentThread.ManagedThreadId}"));
_ = Job.Run(() => Console.WriteLine($"job15 is excuted in thead {Thread.CurrentThread.ManagedThreadId}"));
_ = Job.Run(() => Console.WriteLine($"job16 is excuted in thead {Thread.CurrentThread.ManagedThreadId}"));
}
void ContinueJob()
{
Job.Run(() =>
{
Thread.Sleep(1000);
Console.WriteLine("Foo1");
}).ContinueWith(_ =>
{
Thread.Sleep(100);
Console.WriteLine("Bar1");
}).ContinueWith(_ =>
{
Thread.Sleep(100);
Console.WriteLine("Baz1");
});
Job.Run(() =>
{
Thread.Sleep(100);
Console.WriteLine("Foo2");
}).ContinueWith(_ =>
{
Thread.Sleep(10);
Console.WriteLine("Bar2");
}).ContinueWith(_ =>
{
Thread.Sleep(10);
Console.WriteLine("Baz2");
});
}
static Job Foo() => new Job(() =>
{
Thread.Sleep(1000);
Console.WriteLine("Foo");
});
static Job Bar() => new Job(() =>
{
Thread.Sleep(100);
Console.WriteLine("Bar");
});
static Job Baz() => new Job(() =>
{
Thread.Sleep(10);
Console.WriteLine("Baz");
});