Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add demo #17

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
8 changes: 8 additions & 0 deletions BillDemo/BillDemo.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>

</Project>
9 changes: 9 additions & 0 deletions BillDemo/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace BillDemo {
class Program {
static void Main(string[] args) {
Console.WriteLine("Hello World!");
}
}
}
17 changes: 17 additions & 0 deletions BillDemo/TaskRunner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using ParallelProcessPractice.Core;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace BillDemo {
class TaskRunner : TaskRunnerBase {
public override void Run(IEnumerable<MyTask> tasks) {
Parallel.ForEach(tasks, new ParallelOptions { MaxDegreeOfParallelism = 11 }, task => {
task.DoStepN(1);
task.DoStepN(2);
task.DoStepN(3);
});
}
}
}
53 changes: 53 additions & 0 deletions BillDemo/TaskRunnerV1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using ParallelProcessPractice.Core;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Channels;
using System.Threading.Tasks;

namespace BillDemo {
class TaskRunnerV1 : TaskRunnerBase {
public override void Run(IEnumerable<MyTask> tasks) {

Task t0 = Task.Run(async () => { foreach (var task in tasks) { await queues[1].Writer.WriteAsync(task); } queues[1].Writer.Complete(); });
Task t1 = DoAllStepNAsync(1);
Task t2 = DoAllStepNAsync(2);
Task t3 = DoAllStepNAsync(3);

//Task.WaitAll(tasks.Select(t => Task.Run(async () =>
//{
// await this.queues[1].Writer.WriteAsync(t);
//})).ToArray());

//queues[1].Writer.Complete();

Task.WaitAll(t1, t2, t3);
}

private Channel<MyTask>[] queues = new Channel<MyTask>[3 + 1]
{
null,
Channel.CreateBounded<MyTask>(5),
Channel.CreateUnbounded<MyTask>(),
Channel.CreateUnbounded<MyTask>(),
};

private async Task DoAllStepNAsync(int step) {
bool last = (step == 3);
List<Task> ts = new List<Task>();
while (await queues[step].Reader.WaitToReadAsync()) {
while (queues[step].Reader.TryRead(out MyTask task)) {
ts.Add(Task.Run(async () =>
{
task.DoStepN(step);
if (!last)
await queues[step + 1].Writer.WriteAsync(task);
}));
}
}
Task.WaitAll(ts.ToArray());
if (!last)
queues[step + 1].Writer.Complete();
}
}
}
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}") = "BillDemo", "BillDemo\BillDemo.csproj", "{F080C41B-CBFA-49C8-844F-DA612F43651B}"
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
{F080C41B-CBFA-49C8-844F-DA612F43651B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F080C41B-CBFA-49C8-844F-DA612F43651B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F080C41B-CBFA-49C8-844F-DA612F43651B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F080C41B-CBFA-49C8-844F-DA612F43651B}.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}
{F080C41B-CBFA-49C8-844F-DA612F43651B} = {B21D6D24-8EC2-497F-AE16-E0155FEE28CE}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {15051360-3A56-4052-A944-97C62F90EEC6}
Expand Down