Created
June 30, 2020 00:41
-
-
Save AbhinavPradeep/69c5cadc0db98135da061dd70fd725cc to your computer and use it in GitHub Desktop.
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
using System; | |
using System.Threading; | |
namespace MultiThreadedProgramming | |
{ | |
class Account | |
{ | |
public Account() | |
{ | |
Balance = 0.00; | |
Padlock = new Object(); | |
} | |
public Double Balance{get; private set;} | |
public Object Padlock; | |
public void Withdraw(double Amount) | |
{ | |
lock (Padlock) | |
{ | |
System.Console.WriteLine($"Trying to withdraw ${Amount}"); | |
if (Balance >= Amount) | |
{ | |
Thread.Sleep(10000); | |
Balance -= Amount; | |
System.Console.WriteLine($"Balance left after withdrawl = ${Balance}"); | |
} | |
else | |
{ | |
System.Console.WriteLine($"Sorry you only have ${Balance} left"); | |
} | |
} | |
} | |
public void Deposit(double Amount) | |
{ | |
Balance+= Amount; | |
System.Console.WriteLine($"Balance = ${Balance}"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment