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

CSharp_顾朗哲_无21 #19

Open
wants to merge 1 commit into
base: main
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
26 changes: 26 additions & 0 deletions hw1/.vscode/launch.json
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"
}
]
}
41 changes: 41 additions & 0 deletions hw1/.vscode/tasks.json
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"
}
]
}
225 changes: 138 additions & 87 deletions hw1/hw1/Program.cs
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();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

锁应当作为字段,否则每次调用方法都是对一个全新的锁进行占用,这毫无意义

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) {
Copy link
Owner

Choose a reason for hiding this comment

The 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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

finishedTime 的意义是?
isAtReady 应当更新

}
}

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
*/
}
Loading