From 42d23d47f22548e6b53de508b6429a2ba215827b Mon Sep 17 00:00:00 2001 From: Guan Date: Tue, 3 May 2022 12:55:36 +0800 Subject: [PATCH] add GuanLin runner --- GuanLinDemo/GuanLinDemo.csproj | 12 +++++++ GuanLinDemo/Program.cs | 58 ++++++++++++++++++++++++++++++++++ ParallelProcessPractice.sln | 7 ++++ 3 files changed, 77 insertions(+) create mode 100644 GuanLinDemo/GuanLinDemo.csproj create mode 100644 GuanLinDemo/Program.cs diff --git a/GuanLinDemo/GuanLinDemo.csproj b/GuanLinDemo/GuanLinDemo.csproj new file mode 100644 index 0000000..3a8421a --- /dev/null +++ b/GuanLinDemo/GuanLinDemo.csproj @@ -0,0 +1,12 @@ + + + + Exe + netcoreapp3.1 + + + + + + + diff --git a/GuanLinDemo/Program.cs b/GuanLinDemo/Program.cs new file mode 100644 index 0000000..cfd3918 --- /dev/null +++ b/GuanLinDemo/Program.cs @@ -0,0 +1,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 _semaphoreSlimList; + + + public GuanlinTaskRunner1() + { + _maxConcurrency = new[] { 0, 5, 3, 3 }; + _semaphoreSlimList = new List{ + null, + new SemaphoreSlim(_maxConcurrency[1]), + new SemaphoreSlim(_maxConcurrency[2]), + new SemaphoreSlim(_maxConcurrency[3]) + }; + } + + public override void Run(IEnumerable 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(); + } + } + } + } +} \ No newline at end of file diff --git a/ParallelProcessPractice.sln b/ParallelProcessPractice.sln index 387a9fb..82061a7 100644 --- a/ParallelProcessPractice.sln +++ b/ParallelProcessPractice.sln @@ -47,6 +47,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "NathanDemo", "NathanDemo\Na EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JolinDemo", "JolinDemo\JolinDemo.csproj", "{C58EA4C0-FFEC-43D6-97ED-EF755AE14B64}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GuanLinDemo", "GuanLinDemo\GuanLinDemo.csproj", "{40C57168-5387-452E-BE15-14A86B69D167}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -117,6 +119,10 @@ Global {C58EA4C0-FFEC-43D6-97ED-EF755AE14B64}.Debug|Any CPU.Build.0 = Debug|Any CPU {C58EA4C0-FFEC-43D6-97ED-EF755AE14B64}.Release|Any CPU.ActiveCfg = Release|Any CPU {C58EA4C0-FFEC-43D6-97ED-EF755AE14B64}.Release|Any CPU.Build.0 = Release|Any CPU + {40C57168-5387-452E-BE15-14A86B69D167}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {40C57168-5387-452E-BE15-14A86B69D167}.Debug|Any CPU.Build.0 = Debug|Any CPU + {40C57168-5387-452E-BE15-14A86B69D167}.Release|Any CPU.ActiveCfg = Release|Any CPU + {40C57168-5387-452E-BE15-14A86B69D167}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -136,6 +142,7 @@ Global {1B34613F-9384-4365-A54E-D9719D6E902B} = {B21D6D24-8EC2-497F-AE16-E0155FEE28CE} {CD021A28-2C35-4FAA-BFDC-3E4543F009A0} = {B21D6D24-8EC2-497F-AE16-E0155FEE28CE} {C58EA4C0-FFEC-43D6-97ED-EF755AE14B64} = {B21D6D24-8EC2-497F-AE16-E0155FEE28CE} + {40C57168-5387-452E-BE15-14A86B69D167} = {B21D6D24-8EC2-497F-AE16-E0155FEE28CE} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {15051360-3A56-4052-A944-97C62F90EEC6}