Skip to content

Commit a0374ea

Browse files
committed
2 parents 82bb059 + a797bc9 commit a0374ea

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212
/src/BuggyDemoWeb/bin
1313
/src/BuggyDemoWeb/obj
1414
/src/BuggyDemoWeb/Properties/
15+
/src/BuggyDemoWeb/Controllers/UnresponsiveLowCPUController.cs

src/BuggyDemoWeb/Controllers/UnresponsiveLowCPUController.cs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ namespace BuggyDemoWeb.Controllers
1515
public class UnresponsiveLowCPUController : Controller
1616
{
1717
private readonly LegacyService legacyService;
18-
private Account _accountChecking;
19-
private Account _accountSaving;
2018

2119
public UnresponsiveLowCPUController(LegacyService legacyService)
2220
{
@@ -155,20 +153,37 @@ public IActionResult CreateDeadlockTaskYieldReturn()
155153

156154
return Ok();
157155
}
158-
159-
/// <summary>
160-
/// Classic deadlock... Thread A locked waiting on Thread B, and vice versa
161-
/// </summary>
162-
/// <returns></returns>
156+
163157
[HttpGet("lowcpu/deadlocked-tasks-v4")]
164-
public IActionResult TypicalDeadlockIssue()
158+
public IActionResult MoneyTransfer()
165159
{
166-
legacyService.Transfer(_accountChecking, _accountSaving, 20);
167-
legacyService.Transfer(_accountSaving, _accountChecking, 50);
160+
Transfer(_accountChecking, _accountSaving, 20);
161+
Transfer(_accountSaving, _accountChecking, 50);
168162

169163
return Ok();
170164
}
171165

166+
private Account _accountChecking;
167+
private Account _accountSaving;
168+
169+
public Task Transfer(Account fromaccount, Account toaccount, int sum)
170+
{
171+
var task = Task.Run(() =>
172+
{
173+
lock (fromaccount)
174+
{
175+
Thread.Sleep(1000);
176+
lock (toaccount)
177+
{
178+
fromaccount.Balance -= sum;
179+
toaccount.Balance += sum;
180+
}
181+
}
182+
});
183+
184+
return task;
185+
}
186+
172187
[HttpGet("lowcpu/delayed-data-retrieval/{delay}")]
173188
public async Task<IActionResult> RetrievingDataWithDelay(int delay = 0)
174189
{

0 commit comments

Comments
 (0)