-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathProgram.cs
58 lines (52 loc) · 1.53 KB
/
Program.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using ParallelProcessPractice.Core;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace GuanLinDemo
{
class Program
{
static void Main(string[] args)
{
var guanlinTaskRunner = new GuanlinTaskRunner1();
guanlinTaskRunner.ExecuteTasks(1000);
}
}
public class GuanlinTaskRunner1 : TaskRunnerBase
{
private readonly int[] _maxConcurrency;
private readonly List<SemaphoreSlim> _semaphoreSlimList;
public GuanlinTaskRunner1()
{
_maxConcurrency = new[] { 0, 5, 3, 3 };
_semaphoreSlimList = new List<SemaphoreSlim>{
null,
new SemaphoreSlim(_maxConcurrency[1]),
new SemaphoreSlim(_maxConcurrency[2]),
new SemaphoreSlim(_maxConcurrency[3])
};
}
public override void Run(IEnumerable<MyTask> tasks)
{
Task.WaitAll(tasks.Select(task => Task.Factory.StartNew(() => { GetStepAndRunTask(task); })).ToArray());
}
private void GetStepAndRunTask(MyTask task)
{
for (var i = 1; i <= 3; i++)
{
try
{
_semaphoreSlimList[i].Wait();
task.DoStepN(i);
}
finally
{
_semaphoreSlimList[i].Release();
}
}
}
}
}