-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSynchronizationExample.java
More file actions
45 lines (38 loc) · 1.4 KB
/
Copy pathSynchronizationExample.java
File metadata and controls
45 lines (38 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class BankAccount {
private int balance = 5000; // Initial balance
// Synchronized method to prevent race condition
public synchronized void withdraw(String name, int amount) {
if (balance >= amount) {
System.out.println(name + " is withdrawing $" + amount);
try { Thread.sleep(1000); } catch (InterruptedException e) {}
balance -= amount;
System.out.println(name + " successfully withdrew $" + amount);
} else {
System.out.println(name + " tried to withdraw $" + amount + " but insufficient balance!");
}
System.out.println("Current Balance: $" + balance);
}
}
class Customer extends Thread {
private BankAccount account;
private String name;
private int amount;
public Customer(BankAccount account, String customerName, int withdrawAmount) {
this.account = account;
this.name = customerName;
this.amount = withdrawAmount;
}
public void run() {
account.withdraw(name, amount);
}
}
public class SynchronizationExample {
public static void main(String[] args) {
BankAccount account = new BankAccount();
// Creating two customer threads trying to withdraw money
Customer user1 = new Customer(account, "Alice", 3000);
Customer user2 = new Customer(account, "Bob", 3000);
user1.start();
user2.start();
}
}