-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadSynchronizationExample.java
More file actions
58 lines (48 loc) · 1.68 KB
/
ThreadSynchronizationExample.java
File metadata and controls
58 lines (48 loc) · 1.68 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
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
Write a program for thread synchronization.
*/
class BankAccount {
private int balance;
BankAccount(int initialBalance) {
balance = initialBalance;
}
// synchronized method to ensure only one thread can withdraw at a time
public synchronized void withdraw(int amount, String threadName) {
if (balance >= amount) {
System.out.println(threadName + " is withdrawing " + amount);
balance -= amount;
// Simulate some delay
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println(e);
}
System.out.println(threadName + " completed withdrawal. Remaining balance: " + balance);
} else {
System.out.println(threadName + " attempted to withdraw " + amount + " but insufficient balance. Remaining balance: " + balance);
}
}
}
class WithdrawThread implements Runnable {
private BankAccount account;
private int amount;
private String name;
WithdrawThread(BankAccount account, int amount, String name) {
this.account = account;
this.amount = amount;
this.name = name;
}
public void run() {
account.withdraw(amount, name);
}
}
public class ThreadSynchronizationExample {
public static void main(String[] args) {
BankAccount account = new BankAccount(1000); // initial balance
// Two threads trying to withdraw money concurrently
Thread t1 = new Thread(new WithdrawThread(account, 700, "Thread 1"));
Thread t2 = new Thread(new WithdrawThread(account, 500, "Thread 2"));
t1.start();
t2.start();
}
}