Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions GuanLinDemo/GuanLinDemo.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\ParallelProcessPractice.Core\ParallelProcessPractice.Core.csproj" />
</ItemGroup>

</Project>
58 changes: 58 additions & 0 deletions GuanLinDemo/Program.cs
Original file line number Diff line number Diff line change
@@ -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<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();
}
}
}
}
}
7 changes: 7 additions & 0 deletions ParallelProcessPractice.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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}
Expand Down