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
23 changes: 23 additions & 0 deletions bin/Debug/net8.0/homework.deps.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"runtimeTarget": {
"name": ".NETCoreApp,Version=v8.0",
"signature": ""
},
"compilationOptions": {},
"targets": {
".NETCoreApp,Version=v8.0": {
"homework/1.0.0": {
"runtime": {
"homework.dll": {}
}
}
}
},
"libraries": {
"homework/1.0.0": {
"type": "project",
"serviceable": false,
"sha512": ""
}
}
}
Binary file added bin/Debug/net8.0/homework.dll
Binary file not shown.
Binary file added bin/Debug/net8.0/homework.exe
Binary file not shown.
Binary file added bin/Debug/net8.0/homework.pdb
Binary file not shown.
12 changes: 12 additions & 0 deletions bin/Debug/net8.0/homework.runtimeconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"runtimeOptions": {
"tfm": "net8.0",
"framework": {
"name": "Microsoft.NETCore.App",
"version": "8.0.0"
},
"configProperties": {
"System.Runtime.Serialization.EnableUnsafeBinaryFormatterSerialization": false
}
}
}
41 changes: 37 additions & 4 deletions homework.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,27 @@ public async Task UpdateInventoryAsync(string title, int quantity)
await Task.Delay(100); // 模拟网络延迟

// TODO: 使用 lock 语句保证线程安全
// 提示:在 lock 块中查找书籍并更新库存,若库存不足则输出提示
// 提示:在 lock 块中查找书籍并更新库存,若库存不足则输出提示
lock (_lock)
{
var book = _books.Find(b => b.Title == title);
if (book != null)
{
if (book.Inventory >= quantity)
{
book.Inventory -= quantity;
Console.WriteLine($"成功购买 {quantity} 本《{title}》。");
}
else
{
Console.WriteLine($"库存不足,无法购买 {quantity} 本《{title}》。当前库存:{book.Inventory} 本。");
}
}
else
{
Console.WriteLine($"书籍《{title}》不存在。");
}
}
}
}

Expand All @@ -42,6 +62,14 @@ public class BookStore
// TODO: 实现异步购书方法CheckoutAsync,调用 UpdateInventoryAsync
public async Task CheckoutAsync(string bookTitle, int quantity)
{
try
{
await _db.UpdateInventoryAsync(bookTitle, quantity);
}
catch (Exception ex)
{
Console.WriteLine($"购买《{bookTitle}》时出错:{ex.Message}");
}
}

public async Task SimulateMultipleUsers()
Expand All @@ -50,7 +78,7 @@ public async Task SimulateMultipleUsers()
Console.WriteLine("当前书店库存:");
foreach (var book in books)
{
Console.WriteLine($"- {book.Title}:{book.Inventory} 本");
Console.WriteLine($"- {book.Title}:{book.Inventory} 本");
}

Console.WriteLine("\n 开始模拟多用户购书...\n");
Expand All @@ -59,9 +87,14 @@ public async Task SimulateMultipleUsers()
// 提示:创建多个 Task 调用 CheckoutAsync,并传入不同书名和数量
var tasks = new List<Task>
{

CheckoutAsync("C#入门", 2),
CheckoutAsync("C#入门", 3),
CheckoutAsync("异步编程", 1),
CheckoutAsync("异步编程", 2),
CheckoutAsync("异步编程", 3),
};

await Task.WhenAll(tasks);
Console.WriteLine("\n多用户购书模拟结束。");

Console.WriteLine("\n购买后库存:");
books = await _db.GetBooksAsync();
Expand Down
7 changes: 7 additions & 0 deletions homework.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>AsyncAndMultiThread</RootNamespace>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// <autogenerated />
using System;
using System.Reflection;
[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETCoreApp,Version=v8.0", FrameworkDisplayName = ".NET 8.0")]
Binary file added obj/Debug/net8.0/apphost.exe
Binary file not shown.
22 changes: 22 additions & 0 deletions obj/Debug/net8.0/homework.AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Reflection;

[assembly: System.Reflection.AssemblyCompanyAttribute("homework")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+000c492122464e8ce53d0a32f71af681a0a553fb")]
[assembly: System.Reflection.AssemblyProductAttribute("homework")]
[assembly: System.Reflection.AssemblyTitleAttribute("homework")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]

// 由 MSBuild WriteCodeFragment 类生成。

1 change: 1 addition & 0 deletions obj/Debug/net8.0/homework.AssemblyInfoInputs.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
16fb429f0a68058a65eedfefc3b497af4dc9784d5eeedec415841e93246c668d
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
is_global = true
build_property.TargetFramework = net8.0
build_property.TargetPlatformMinVersion =
build_property.UsingMicrosoftNETSdkWeb =
build_property.ProjectTypeGuids =
build_property.InvariantGlobalization =
build_property.PlatformNeutralAssembly =
build_property.EnforceExtendedAnalyzerRules =
build_property._SupportedPlatformList = Linux,macOS,Windows
build_property.RootNamespace = AsyncAndMultiThread
build_property.ProjectDir = D:\Desktop\2025软件部暑培\作业\Async-and-Multi_thread\
build_property.EnableComHosting =
build_property.EnableGeneratedComInterfaceComImportInterop =
build_property.EffectiveAnalysisLevelStyle = 8.0
build_property.EnableCodeStyleSeverity =
Binary file added obj/Debug/net8.0/homework.assets.cache
Binary file not shown.
1 change: 1 addition & 0 deletions obj/Debug/net8.0/homework.csproj.CoreCompileInputs.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
b94099462aa5b357f24733944fa2d231b9750d4b58effd7099125268ab512e6c
15 changes: 15 additions & 0 deletions obj/Debug/net8.0/homework.csproj.FileListAbsolute.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
D:\Desktop\2025软件部暑培\作业\Async-and-Multi_thread\bin\Debug\net8.0\homework.exe
D:\Desktop\2025软件部暑培\作业\Async-and-Multi_thread\bin\Debug\net8.0\homework.deps.json
D:\Desktop\2025软件部暑培\作业\Async-and-Multi_thread\bin\Debug\net8.0\homework.runtimeconfig.json
D:\Desktop\2025软件部暑培\作业\Async-and-Multi_thread\bin\Debug\net8.0\homework.dll
D:\Desktop\2025软件部暑培\作业\Async-and-Multi_thread\bin\Debug\net8.0\homework.pdb
D:\Desktop\2025软件部暑培\作业\Async-and-Multi_thread\obj\Debug\net8.0\homework.GeneratedMSBuildEditorConfig.editorconfig
D:\Desktop\2025软件部暑培\作业\Async-and-Multi_thread\obj\Debug\net8.0\homework.AssemblyInfoInputs.cache
D:\Desktop\2025软件部暑培\作业\Async-and-Multi_thread\obj\Debug\net8.0\homework.AssemblyInfo.cs
D:\Desktop\2025软件部暑培\作业\Async-and-Multi_thread\obj\Debug\net8.0\homework.csproj.CoreCompileInputs.cache
D:\Desktop\2025软件部暑培\作业\Async-and-Multi_thread\obj\Debug\net8.0\homework.sourcelink.json
D:\Desktop\2025软件部暑培\作业\Async-and-Multi_thread\obj\Debug\net8.0\homework.dll
D:\Desktop\2025软件部暑培\作业\Async-and-Multi_thread\obj\Debug\net8.0\refint\homework.dll
D:\Desktop\2025软件部暑培\作业\Async-and-Multi_thread\obj\Debug\net8.0\homework.pdb
D:\Desktop\2025软件部暑培\作业\Async-and-Multi_thread\obj\Debug\net8.0\homework.genruntimeconfig.cache
D:\Desktop\2025软件部暑培\作业\Async-and-Multi_thread\obj\Debug\net8.0\ref\homework.dll
Binary file added obj/Debug/net8.0/homework.dll
Binary file not shown.
1 change: 1 addition & 0 deletions obj/Debug/net8.0/homework.genruntimeconfig.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
9ba967bfcf387788cce77f23d4758167084e5e84cf6018b6e1eccbdd587f368d
Binary file added obj/Debug/net8.0/homework.pdb
Binary file not shown.
1 change: 1 addition & 0 deletions obj/Debug/net8.0/homework.sourcelink.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"documents":{"D:\\Desktop\\2025软件部暑培\\作业\\Async-and-Multi_thread\\*":"https://raw.githubusercontent.com/Hang-THU/Async-and-Multi_thread/000c492122464e8ce53d0a32f71af681a0a553fb/*"}}
Binary file added obj/Debug/net8.0/ref/homework.dll
Binary file not shown.
Binary file added obj/Debug/net8.0/refint/homework.dll
Binary file not shown.
73 changes: 73 additions & 0 deletions obj/homework.csproj.nuget.dgspec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
"format": 1,
"restore": {
"D:\\Desktop\\2025软件部暑培\\作业\\Async-and-Multi_thread\\homework.csproj": {}
},
"projects": {
"D:\\Desktop\\2025软件部暑培\\作业\\Async-and-Multi_thread\\homework.csproj": {
"version": "1.0.0",
"restore": {
"projectUniqueName": "D:\\Desktop\\2025软件部暑培\\作业\\Async-and-Multi_thread\\homework.csproj",
"projectName": "homework",
"projectPath": "D:\\Desktop\\2025软件部暑培\\作业\\Async-and-Multi_thread\\homework.csproj",
"packagesPath": "C:\\Users\\Yang\\.nuget\\packages\\",
"outputPath": "D:\\Desktop\\2025软件部暑培\\作业\\Async-and-Multi_thread\\obj\\",
"projectStyle": "PackageReference",
"fallbackFolders": [
"C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\NuGetPackages"
],
"configFilePaths": [
"C:\\Users\\Yang\\AppData\\Roaming\\NuGet\\NuGet.Config",
"C:\\Program Files (x86)\\NuGet\\Config\\Microsoft.VisualStudio.FallbackLocation.config",
"C:\\Program Files (x86)\\NuGet\\Config\\Microsoft.VisualStudio.Offline.config"
],
"originalTargetFrameworks": [
"net8.0"
],
"sources": {
"C:\\Program Files (x86)\\Microsoft SDKs\\NuGetPackages\\": {},
"https://api.nuget.org/v3/index.json": {}
},
"frameworks": {
"net8.0": {
"targetAlias": "net8.0",
"projectReferences": {}
}
},
"warningProperties": {
"warnAsError": [
"NU1605"
]
},
"restoreAuditProperties": {
"enableAudit": "true",
"auditLevel": "low",
"auditMode": "direct"
},
"SdkAnalysisLevel": "9.0.200"
},
"frameworks": {
"net8.0": {
"targetAlias": "net8.0",
"imports": [
"net461",
"net462",
"net47",
"net471",
"net472",
"net48",
"net481"
],
"assetTargetFallback": true,
"warn": true,
"frameworkReferences": {
"Microsoft.NETCore.App": {
"privateAssets": "all"
}
},
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\9.0.203/PortableRuntimeIdentifierGraph.json"
}
}
}
}
}
16 changes: 16 additions & 0 deletions obj/homework.csproj.nuget.g.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<RestoreSuccess Condition=" '$(RestoreSuccess)' == '' ">True</RestoreSuccess>
<RestoreTool Condition=" '$(RestoreTool)' == '' ">NuGet</RestoreTool>
<ProjectAssetsFile Condition=" '$(ProjectAssetsFile)' == '' ">$(MSBuildThisFileDirectory)project.assets.json</ProjectAssetsFile>
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">$(UserProfile)\.nuget\packages\</NuGetPackageRoot>
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">C:\Users\Yang\.nuget\packages\;C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages</NuGetPackageFolders>
<NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle>
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.13.2</NuGetToolVersion>
</PropertyGroup>
<ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
<SourceRoot Include="C:\Users\Yang\.nuget\packages\" />
<SourceRoot Include="C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages\" />
</ItemGroup>
</Project>
2 changes: 2 additions & 0 deletions obj/homework.csproj.nuget.g.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" />
79 changes: 79 additions & 0 deletions obj/project.assets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{
"version": 3,
"targets": {
"net8.0": {}
},
"libraries": {},
"projectFileDependencyGroups": {
"net8.0": []
},
"packageFolders": {
"C:\\Users\\Yang\\.nuget\\packages\\": {},
"C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\NuGetPackages": {}
},
"project": {
"version": "1.0.0",
"restore": {
"projectUniqueName": "D:\\Desktop\\2025软件部暑培\\作业\\Async-and-Multi_thread\\homework.csproj",
"projectName": "homework",
"projectPath": "D:\\Desktop\\2025软件部暑培\\作业\\Async-and-Multi_thread\\homework.csproj",
"packagesPath": "C:\\Users\\Yang\\.nuget\\packages\\",
"outputPath": "D:\\Desktop\\2025软件部暑培\\作业\\Async-and-Multi_thread\\obj\\",
"projectStyle": "PackageReference",
"fallbackFolders": [
"C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\NuGetPackages"
],
"configFilePaths": [
"C:\\Users\\Yang\\AppData\\Roaming\\NuGet\\NuGet.Config",
"C:\\Program Files (x86)\\NuGet\\Config\\Microsoft.VisualStudio.FallbackLocation.config",
"C:\\Program Files (x86)\\NuGet\\Config\\Microsoft.VisualStudio.Offline.config"
],
"originalTargetFrameworks": [
"net8.0"
],
"sources": {
"C:\\Program Files (x86)\\Microsoft SDKs\\NuGetPackages\\": {},
"https://api.nuget.org/v3/index.json": {}
},
"frameworks": {
"net8.0": {
"targetAlias": "net8.0",
"projectReferences": {}
}
},
"warningProperties": {
"warnAsError": [
"NU1605"
]
},
"restoreAuditProperties": {
"enableAudit": "true",
"auditLevel": "low",
"auditMode": "direct"
},
"SdkAnalysisLevel": "9.0.200"
},
"frameworks": {
"net8.0": {
"targetAlias": "net8.0",
"imports": [
"net461",
"net462",
"net47",
"net471",
"net472",
"net48",
"net481"
],
"assetTargetFallback": true,
"warn": true,
"frameworkReferences": {
"Microsoft.NETCore.App": {
"privateAssets": "all"
}
},
"runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\9.0.203/PortableRuntimeIdentifierGraph.json"
}
}
}
}
8 changes: 8 additions & 0 deletions obj/project.nuget.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"version": 2,
"dgSpecHash": "cvrFL12EBMQ=",
"success": true,
"projectFilePath": "D:\\Desktop\\2025软件部暑培\\作业\\Async-and-Multi_thread\\homework.csproj",
"expectedPackageFiles": [],
"logs": []
}