-
Notifications
You must be signed in to change notification settings - Fork 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
CSharp_顾朗哲_无21 #19
Open
Grange007
wants to merge
1
commit into
shangfengh:main
Choose a base branch
from
Grange007: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
CSharp_顾朗哲_无21 #19
Changes from all commits
Commits
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 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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
// Use IntelliSense to find out which attributes exist for C# debugging | ||
// Use hover for the description of the existing attributes | ||
// For further information visit https://github.com/dotnet/vscode-csharp/blob/main/debugger-launchjson.md | ||
"name": ".NET Core Launch (console)", | ||
"type": "coreclr", | ||
"request": "launch", | ||
"preLaunchTask": "build", | ||
// If you have changed target frameworks, make sure to update the program path. | ||
"program": "${workspaceFolder}/hw1/bin/Debug/net6.0/hw1.dll", | ||
"args": [], | ||
"cwd": "${workspaceFolder}/hw1", | ||
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console | ||
"console": "internalConsole", | ||
"stopAtEntry": false | ||
}, | ||
{ | ||
"name": ".NET Core Attach", | ||
"type": "coreclr", | ||
"request": "attach" | ||
} | ||
] | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"version": "2.0.0", | ||
"tasks": [ | ||
{ | ||
"label": "build", | ||
"command": "dotnet", | ||
"type": "process", | ||
"args": [ | ||
"build", | ||
"${workspaceFolder}/hw1/hw1.csproj", | ||
"/property:GenerateFullPaths=true", | ||
"/consoleloggerparameters:NoSummary" | ||
], | ||
"problemMatcher": "$msCompile" | ||
}, | ||
{ | ||
"label": "publish", | ||
"command": "dotnet", | ||
"type": "process", | ||
"args": [ | ||
"publish", | ||
"${workspaceFolder}/hw1/hw1.csproj", | ||
"/property:GenerateFullPaths=true", | ||
"/consoleloggerparameters:NoSummary" | ||
], | ||
"problemMatcher": "$msCompile" | ||
}, | ||
{ | ||
"label": "watch", | ||
"command": "dotnet", | ||
"type": "process", | ||
"args": [ | ||
"watch", | ||
"run", | ||
"--project", | ||
"${workspaceFolder}/hw1/hw1.csproj" | ||
], | ||
"problemMatcher": "$msCompile" | ||
} | ||
] | ||
} |
This file contains 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,88 +1,139 @@ | ||
using System; | ||
using System.Diagnostics; | ||
|
||
namespace Homework | ||
{ | ||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
ILongProgressByTime a = new LongProgressByTime(); | ||
new Thread | ||
( | ||
() => | ||
{ | ||
Console.WriteLine("A Start: "+(a.Start(2000)).ToString()); | ||
Thread.Sleep(1000); | ||
Console.WriteLine("A TrySet0: "+(a.TrySet0()).ToString()); | ||
Thread.Sleep(500); | ||
Console.WriteLine("A Start: "+(a.Start(1000)).ToString() +" Now: "+Environment.TickCount64); | ||
Thread.Sleep(500); | ||
Console.WriteLine("A Progress: "+(a.GetProgress()).ToString() +" Now: "+Environment.TickCount64); | ||
Thread.Sleep(1003); | ||
Console.WriteLine("A TrySet0: "+(a.TrySet0()).ToString()); | ||
} | ||
).Start(); | ||
|
||
new Thread | ||
( | ||
() => | ||
{ | ||
Console.WriteLine("B Start: "+(a.Start(2000)).ToString()); | ||
Thread.Sleep(1500); | ||
Console.WriteLine("B Start: " +(a.Start(1000)).ToString() + " Now: " + Environment.TickCount64); | ||
Thread.Sleep(500); | ||
Console.WriteLine("B Progress: " +(a.GetProgress()).ToString() + " Now: " + Environment.TickCount64); | ||
} | ||
).Start(); | ||
} | ||
} | ||
|
||
public interface ILongProgressByTime | ||
{ | ||
/// <summary> | ||
/// 尝试加载下一次进度条,needTime指再次加载进度条所需时间,单位毫秒 | ||
/// 如果之前进度条处于就绪态,则将进度开始下一次加载,返回true | ||
/// 如果之前进度条不处于就绪态,返回false | ||
/// </summary> | ||
public bool Start(long needTime); | ||
|
||
/// <summary> | ||
/// 使未完成的进度条清零并终止变为就绪态,返回值代表是否成功终止 | ||
/// </summary> | ||
public bool TrySet0(); | ||
|
||
/// <summary> | ||
/// 使进度条强制清零并终止变为就绪态 | ||
/// </summary> | ||
public void Set0(); | ||
|
||
/// <summary> | ||
/// ElapsedTime指其中已过去的时间,NeedTime指当前Progress完成所需时间,单位毫秒 | ||
/// </summary> | ||
public (long ElapsedTime, long NeedTime) GetProgress(); | ||
} | ||
|
||
public class LongProgressByTime: ILongProgressByTime | ||
{ | ||
// 根据时间推算Start后完成多少进度的进度条(long)。 | ||
|
||
// 只允许修改LongProgressByTime类中的代码 | ||
// 要求实现ILongProgressByTime中的要求 | ||
// 可利用Environment.TickCount64获取当前时间(单位ms) | ||
|
||
//挑战:利用原子操作 | ||
//long.MaxValue非常久 | ||
} | ||
|
||
/*输出示例(仅供参考): | ||
* A Start: False | ||
B Start: True | ||
A TrySet0: True | ||
B Start: True Now: 14536562 | ||
A Start: False Now: 14536578 | ||
B Progress: (516, 1000) Now: 14537078 | ||
A Progress: (516, 1000) Now: 14537078 | ||
A TrySet0: False | ||
*/ | ||
using System; | ||
using System.Diagnostics; | ||
|
||
namespace Homework | ||
{ | ||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
ILongProgressByTime a = new LongProgressByTime(); | ||
new Thread | ||
( | ||
() => | ||
{ | ||
Console.WriteLine("A Start: "+(a.Start(2000)).ToString()); | ||
Thread.Sleep(1000); | ||
Console.WriteLine("A TrySet0: "+(a.TrySet0()).ToString()); | ||
Thread.Sleep(500); | ||
Console.WriteLine("A Start: "+(a.Start(1000)).ToString() +" Now: "+Environment.TickCount64); | ||
Thread.Sleep(500); | ||
Console.WriteLine("A Progress: "+(a.GetProgress()).ToString() +" Now: "+Environment.TickCount64); | ||
Thread.Sleep(1003); | ||
Console.WriteLine("A TrySet0: "+(a.TrySet0()).ToString()); | ||
} | ||
).Start(); | ||
|
||
new Thread | ||
( | ||
() => | ||
{ | ||
Console.WriteLine("B Start: "+(a.Start(2000)).ToString()); | ||
Thread.Sleep(1500); | ||
Console.WriteLine("B Start: " +(a.Start(1000)).ToString() + " Now: " + Environment.TickCount64); | ||
Thread.Sleep(500); | ||
Console.WriteLine("B Progress: " +(a.GetProgress()).ToString() + " Now: " + Environment.TickCount64); | ||
} | ||
).Start(); | ||
} | ||
} | ||
|
||
public interface ILongProgressByTime | ||
{ | ||
/// <summary> | ||
/// 尝试加载下一次进度条,needTime指再次加载进度条所需时间,单位毫秒 | ||
/// 如果之前进度条处于就绪态,则将进度开始下一次加载,返回true | ||
/// 如果之前进度条不处于就绪态,返回false | ||
/// </summary> | ||
public bool Start(long needTime); | ||
|
||
/// <summary> | ||
/// 使未完成的进度条清零并终止变为就绪态,返回值代表是否成功终止 | ||
/// </summary> | ||
public bool TrySet0(); | ||
|
||
/// <summary> | ||
/// 使进度条强制清零并终止变为就绪态 | ||
/// </summary> | ||
public void Set0(); | ||
|
||
/// <summary> | ||
/// ElapsedTime指其中已过去的时间,NeedTime指当前Progress完成所需时间,单位毫秒 | ||
/// </summary> | ||
public (long ElapsedTime, long NeedTime) GetProgress(); | ||
} | ||
|
||
public class LongProgressByTime: ILongProgressByTime | ||
{ | ||
// 根据时间推算Start后完成多少进度的进度条(long)。 | ||
|
||
// 只允许修改LongProgressByTime类中的代码 | ||
// 要求实现ILongProgressByTime中的要求 | ||
// 可利用Environment.TickCount64获取当前时间(单位ms) | ||
|
||
long startTime; | ||
long requiredTime; | ||
long finishedTime; | ||
bool isAtReady = true; | ||
|
||
public bool Start(long needTime) { | ||
object lockobject = new object(); | ||
lock (lockobject) { | ||
if (isAtReady) { | ||
isAtReady = false; | ||
requiredTime = needTime; | ||
startTime = Environment.TickCount64; | ||
finishedTime = 0; | ||
return true; | ||
} | ||
else { | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
public bool TrySet0() { | ||
object lockobject = new object(); | ||
lock (lockobject) { | ||
if (!isAtReady) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 应当“使未完成的进度条清零” |
||
isAtReady = true; | ||
finishedTime = 0; | ||
return true; | ||
} | ||
else { | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
public void Set0() { | ||
object lockobject = new object(); | ||
lock (lockobject) { | ||
finishedTime = 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. finishedTime 的意义是? |
||
} | ||
} | ||
|
||
public (long ElapsedTime, long NeedTime) GetProgress() { | ||
object lockobject = new object(); | ||
lock (lockobject) { | ||
long nowTime = Environment.TickCount64; | ||
finishedTime = nowTime - startTime; | ||
return (finishedTime, requiredTime); | ||
} | ||
} | ||
|
||
//挑战:利用原子操作 | ||
//long.MaxValue非常久 | ||
} | ||
|
||
/*输出示例(仅供参考): | ||
* A Start: False | ||
B Start: True | ||
A TrySet0: True | ||
B Start: True Now: 14536562 | ||
A Start: False Now: 14536578 | ||
B Progress: (516, 1000) Now: 14537078 | ||
A Progress: (516, 1000) Now: 14537078 | ||
A TrySet0: False | ||
*/ | ||
} |
Oops, something went wrong.
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.
锁应当作为字段,否则每次调用方法都是对一个全新的锁进行占用,这毫无意义