Mid
From PDF
Power Questions
High-Impact Interview Questions
Multi-Threaded Bank System?
public class BankAccount
{
private object _lock = new object();
public decimal Balance { get; private set; }
public void Deposit(decimal amount)
{
lock(_lock)
{
Balance += amount;
}
}
public void Withdraw(decimal amount)
{
lock(_lock)
{
if (Balance >= amount)
Balance -= amount;
}
}
}
Ensures thread safety and prevents financial inconsistency.
Share this Q&A
Share preview image: https://www.toolliyo.com/images/toolliyo-logo.png