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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added .vs/Async-and-Multi_thread/v17/.wsuo
Binary file not shown.
37 changes: 37 additions & 0 deletions .vs/Async-and-Multi_thread/v17/DocumentLayout.backup.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"Version": 1,
"WorkspaceRootPath": "C:\\Users\\32084\\Desktop\\Async-and-Multi_thread\\",
"Documents": [
{
"AbsoluteMoniker": "D:0:0:{A2FE74E1-B743-11D0-AE1A-00A0C90FFFC3}|\u003CMiscFiles\u003E|C:\\Users\\32084\\Desktop\\Async-and-Multi_thread\\homework.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}",
"RelativeMoniker": "D:0:0:{A2FE74E1-B743-11D0-AE1A-00A0C90FFFC3}|\u003CMiscFiles\u003E|solutionrelative:homework.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}"
}
],
"DocumentGroupContainers": [
{
"Orientation": 0,
"VerticalTabListWidth": 256,
"DocumentGroups": [
{
"DockedWidth": 200,
"SelectedChildIndex": 0,
"Children": [
{
"$type": "Document",
"DocumentIndex": 0,
"Title": "homework.cs",
"DocumentMoniker": "C:\\Users\\32084\\Desktop\\Async-and-Multi_thread\\homework.cs",
"RelativeDocumentMoniker": "homework.cs",
"ToolTip": "C:\\Users\\32084\\Desktop\\Async-and-Multi_thread\\homework.cs",
"RelativeToolTip": "homework.cs",
"ViewState": "AgIAAAAAAAAAAAAAAAAAABcAAAArAAAAAAAAAA==",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
"WhenOpened": "2025-08-28T03:16:16.226Z",
"EditorCaption": ""
}
]
}
]
}
]
}
37 changes: 37 additions & 0 deletions .vs/Async-and-Multi_thread/v17/DocumentLayout.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"Version": 1,
"WorkspaceRootPath": "C:\\Users\\32084\\Desktop\\Async-and-Multi_thread\\",
"Documents": [
{
"AbsoluteMoniker": "D:0:0:{A2FE74E1-B743-11D0-AE1A-00A0C90FFFC3}|\u003CMiscFiles\u003E|C:\\Users\\32084\\Desktop\\Async-and-Multi_thread\\homework.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}",
"RelativeMoniker": "D:0:0:{A2FE74E1-B743-11D0-AE1A-00A0C90FFFC3}|\u003CMiscFiles\u003E|solutionrelative:homework.cs||{A6C744A8-0E4A-4FC6-886A-064283054674}"
}
],
"DocumentGroupContainers": [
{
"Orientation": 0,
"VerticalTabListWidth": 256,
"DocumentGroups": [
{
"DockedWidth": 200,
"SelectedChildIndex": 0,
"Children": [
{
"$type": "Document",
"DocumentIndex": 0,
"Title": "homework.cs",
"DocumentMoniker": "C:\\Users\\32084\\Desktop\\Async-and-Multi_thread\\homework.cs",
"RelativeDocumentMoniker": "homework.cs",
"ToolTip": "C:\\Users\\32084\\Desktop\\Async-and-Multi_thread\\homework.cs",
"RelativeToolTip": "homework.cs",
"ViewState": "AgIAAAAAAAAAAAAAAAAAABcAAAArAAAAAAAAAA==",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000738|",
"WhenOpened": "2025-08-28T03:16:16.226Z",
"EditorCaption": ""
}
]
}
]
}
]
}
3 changes: 3 additions & 0 deletions .vs/ProjectSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"CurrentProjectSetting": null
}
7 changes: 7 additions & 0 deletions .vs/VSWorkspaceState.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"ExpandedNodes": [
""
],
"SelectedNode": "\\README.md",
"PreviewInSolutionExplorer": false
}
Binary file added .vs/slnx.sqlite
Binary file not shown.
29 changes: 26 additions & 3 deletions homework.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,25 @@ public Task<List<Book>> GetBooksAsync()
public async Task UpdateInventoryAsync(string title, int quantity)
{
await Task.Delay(100); // 模拟网络延迟

lock (_lock) {
var book = _books.Find(b => b.Title == title);
if (book != null)
{
if (book.Inventory >= quantity)
{
book.Inventory -= quantity;
Console.WriteLine($"成功购买 {quantity} 本《{title}》,剩余库存:{book.Inventory} 本");
}
else
{
Console.WriteLine($"库存不足,无法购买 {quantity} 本《{title}》,当前库存:{book.Inventory} 本");
}
}
else
{
Console.WriteLine($"书籍《{title}》不存在");
}
}
// TODO: 使用 lock 语句保证线程安全
// 提示:在 lock 块中查找书籍并更新库存,若库存不足则输出提示
}
Expand All @@ -42,6 +60,7 @@ public class BookStore
// TODO: 实现异步购书方法CheckoutAsync,调用 UpdateInventoryAsync
public async Task CheckoutAsync(string bookTitle, int quantity)
{
await _db.UpdateInventoryAsync(bookTitle, quantity);
}

public async Task SimulateMultipleUsers()
Expand All @@ -59,10 +78,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购买后库存:");
books = await _db.GetBooksAsync();
foreach (var book in books)
Expand Down