-
Notifications
You must be signed in to change notification settings - Fork 26
异步&多线程-胡纪亨-无35 #18
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
Open
Hujh23
wants to merge
2
commits into
asdawej:main
Choose a base branch
from
Hujh23:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
异步&多线程-胡纪亨-无35 #18
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,125 +1,149 @@ | ||
| namespace HW_Async_Thread; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| public class Program | ||
| namespace HW_Async_Thread | ||
| { | ||
| public static void Main() | ||
| public class Program | ||
| { | ||
| // 测试用例: (a + b) + (c + d) | ||
| // 可以自行修改测试用例 | ||
| ValueExpr a = new(1); | ||
| ValueExpr b = new(2); | ||
| ValueExpr c = new(3); | ||
| ValueExpr d = new(4); | ||
| AddExpr add1 = new(a, b); | ||
| AddExpr add2 = new(c, d); | ||
| AddExpr add3 = new(add1, add2); | ||
| Console.WriteLine(add3.Val); | ||
| a.NewVal = 5; | ||
| Console.WriteLine(add3.Val); | ||
| } | ||
| } | ||
| public static async Task Main() | ||
| { | ||
| // 测试用例: (a + b) + (c + d) | ||
| ValueExpr a = new ValueExpr(1); | ||
| ValueExpr b = new ValueExpr(2); | ||
| ValueExpr c = new ValueExpr(3); | ||
| ValueExpr d = new ValueExpr(4); | ||
| AddExpr add1 = new AddExpr(a, b); | ||
| AddExpr add2 = new AddExpr(c, d); | ||
| AddExpr add3 = new AddExpr(add1, add2); | ||
|
|
||
| /// <summary> | ||
| /// 表达式结点抽象类,用于构造表达式树 | ||
| /// </summary> | ||
| public abstract class Expr | ||
| { | ||
| /// <summary> | ||
| /// 父结点 | ||
| /// </summary> | ||
| protected Expr? parent = null; | ||
|
|
||
| /// <summary> | ||
| /// 表达式的值,只允许返回一个现成的值,可以加锁 | ||
| /// 拓展思考:是否所有时候读取到的值都是其正确的值?如何避免? | ||
| /// </summary> | ||
| public abstract int Val { get; } | ||
|
|
||
| /// <summary> | ||
| /// 异步方法,它的作用是启动一个任务,推动结点自身及其父结点更新值 | ||
| /// 可以根据自身需求适当修改方法签名 | ||
| /// </summary> | ||
| public abstract Task Update(); | ||
|
|
||
| /// <summary> | ||
| /// 注册父结点 | ||
| /// 思考:当父结点被注册后,父结点的值是否需要更新? | ||
| /// </summary> | ||
| /// <param name="parent">待注册的父结点</param> | ||
| public abstract void Register(Expr parent); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// 数据结点 | ||
| /// </summary> | ||
| /// <param name="initVal">初始值</param> | ||
| public class ValueExpr(int initVal) : Expr | ||
| { | ||
| int val = initVal; | ||
| public override int Val | ||
| { | ||
| get | ||
| { | ||
| // TODO 1:读取操作 | ||
| return val; | ||
| Console.WriteLine(await add3.Val); | ||
|
|
||
| a.NewVal = 5; | ||
| await add3.Update(); | ||
|
|
||
|
|
||
| Console.WriteLine(await add3.Val); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// 修改数据 | ||
| /// 思考:修改数据后,父结点是否也需要更新? | ||
| /// </summary> | ||
| public int NewVal | ||
| public abstract class Expr | ||
| { | ||
| set | ||
| protected readonly List<Expr> parents = new List<Expr>(); | ||
| private readonly SemaphoreSlim semaphore = new SemaphoreSlim(1, 1); | ||
| protected bool isDirty = true; | ||
|
|
||
| public abstract Task<int> GetValAsync(); | ||
|
|
||
| public Task<int> Val | ||
| { | ||
| // TODO 2:修改操作 | ||
| get | ||
| { | ||
| return GetValAsync(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public override async Task Update() | ||
| { | ||
| // TODO 3:更新操作 | ||
| } | ||
| public abstract Task Update(); | ||
|
|
||
| public override void Register(Expr parent) | ||
| { | ||
| // TODO 4:注册操作 | ||
| } | ||
| } | ||
| public abstract void Register(Expr parent); | ||
|
|
||
| /// <summary> | ||
| /// 加法表达式结点 | ||
| /// 可以根据自己的想法创造更多种类的结点 | ||
| /// </summary> | ||
| public class AddExpr : Expr | ||
| { | ||
| int val = 0; | ||
| public override int Val | ||
| { | ||
| get | ||
| protected void NotifyParents() | ||
| { | ||
| // TODO 5:读取操作 | ||
| return val; | ||
| foreach (var parent in parents) | ||
| { | ||
| _ = parent.Update(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public Expr ExprA, ExprB; | ||
| public AddExpr(Expr A, Expr B) | ||
| { | ||
| ExprA = A; | ||
| ExprB = B; | ||
| A.Register(this); | ||
| B.Register(this); | ||
| protected async Task WaitForUpdate() | ||
| { | ||
| await semaphore.WaitAsync(); | ||
| try | ||
| { | ||
| await Update(); | ||
| } | ||
| finally | ||
| { | ||
| semaphore.Release(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public override async Task Update() | ||
| public class ValueExpr : Expr | ||
| { | ||
| // TODO 6:更新操作 | ||
| private int val; | ||
|
|
||
| public ValueExpr(int initVal) | ||
| { | ||
| val = initVal; | ||
| } | ||
|
|
||
| public override Task<int> GetValAsync() | ||
| { | ||
| return Task.FromResult(val); | ||
| } | ||
|
|
||
| public int NewVal | ||
| { | ||
| set | ||
| { | ||
| if (val != value) | ||
| { | ||
| val = value; | ||
| NotifyParents(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public override Task Update() | ||
| { | ||
| return Task.CompletedTask; | ||
| } | ||
|
|
||
| public override void Register(Expr parent) | ||
| { | ||
| parents.Add(parent); | ||
| } | ||
| } | ||
|
|
||
| public override void Register(Expr parent) | ||
| public class AddExpr : Expr | ||
| { | ||
| // TODO 7:注册操作 | ||
| private int val; | ||
| private readonly Expr exprA; | ||
| private readonly Expr exprB; | ||
|
|
||
| public AddExpr(Expr A, Expr B) | ||
| { | ||
| exprA = A; | ||
| exprB = B; | ||
| A.Register(this); | ||
| B.Register(this); | ||
| } | ||
|
|
||
| public override async Task<int> GetValAsync() | ||
| { | ||
| if (isDirty) | ||
| { | ||
| await WaitForUpdate(); | ||
| } | ||
| return val; | ||
| } | ||
|
|
||
| public override async Task Update() | ||
| { | ||
| await Task.WhenAll(exprA.GetValAsync(), exprB.GetValAsync()); | ||
| val = await exprA.GetValAsync() + await exprB.GetValAsync(); | ||
| isDirty = false; | ||
| NotifyParents(); | ||
| } | ||
|
|
||
| public override void Register(Expr parent) | ||
| { | ||
| parents.Add(parent); | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个地方可以将前一句等待得到的结果直接拿来用。