Skip to content

Commit 1f5b714

Browse files
committed
feat: implement synchronized ATM access with multiple customers
WHAT the code does: - Defines an ATM class with two synchronized methods: - checkBalance() simulates balance inquiry with delay. - withdraw() simulates cash withdrawal with delay. - Customer class extends Thread: - Holds name, amount, and ATM reference. - Overrides run() to perform both checkBalance() and withdraw(). - ATMMoney main class: - Creates one ATM instance. - Spawns two Customer threads (Smith and John) using the same ATM. - Starts both threads to simulate concurrent ATM usage. WHY this matters: - Demonstrates synchronization in Java to prevent race conditions when multiple threads access shared resources. - Shows how synchronized methods ensure only one thread executes ATM operations at a time. - Models a real-world case where multiple customers safely share a single ATM. HOW it works: 1. ATM methods declared synchronized → implicit lock on ATM instance. 2. Each Customer thread executes useATM(): - First checks balance. - Then withdraws specified amount. 3. Thread.sleep() simulates time delay (like processing transactions). 4. Because of synchronization: - While one customer uses ATM, the other must wait. - Prevents inconsistent or interleaved outputs. Key points: - `synchronized` ensures mutual exclusion. - Threads share one ATM instance → critical section protection. - Thread lifecycle: start() → run() → terminated. Short key: java-threads synchronized-atm-multithreading demo. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent f4859fb commit 1f5b714

File tree

1 file changed

+13
-19
lines changed

1 file changed

+13
-19
lines changed
Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
class ATM
2-
{
3-
synchronized public void checkBalance(String name)
4-
{
1+
class ATM {
2+
synchronized public void checkBalance(String name) {
53
System.out.println(name+" Checking ");
64

75
try{Thread.sleep(1000);}catch (Exception e){}
86

9-
System.out.println("Balance is");
7+
System.out.println("Balance is: ");
108
}
119

12-
synchronized public void withdraw(String name, int amount)
13-
{
10+
synchronized public void withdraw(String name, int amount) {
1411
System.out.println(name+" withdrawing ");
1512

1613
try{Thread.sleep(1000);}catch (Exception e){}
@@ -19,35 +16,32 @@ synchronized public void withdraw(String name, int amount)
1916
}
2017
}
2118

22-
class Customer extends Thread
23-
{
19+
class Customer extends Thread {
2420
String name;
2521
int amount;
2622

27-
ATM atm; //Reference to the ATM
28-
Customer(String n, ATM a, int amt) //prameterized constructor and properties are initialized.
23+
ATM atm; //Reference to the ATM.
24+
Customer(String n, ATM a, int amt) //parameterized constructor and properties are initialized.
2925
{
3026
name=n;
3127
atm=a;
3228
amount=amt;
3329
}
3430

3531
//Method call.
36-
public void useATM()
37-
{
32+
public void useATM() {
3833
atm.checkBalance(name);
3934
atm.withdraw(name, amount);
4035
}
41-
public void run() //Must override to acheive MultiThreading
36+
37+
public void run() //Must override to achieve MultiThreading.
4238
{
4339
useATM();
4440
}
4541
}
4642

47-
public class ATMMoney
48-
{
49-
public static void main(String[] args)
50-
{
43+
public class ATMMoney {
44+
public static void main(String[] args) {
5145
ATM atm = new ATM();
5246

5347
Customer c1=new Customer("Smith", atm,100);
@@ -56,4 +50,4 @@ public static void main(String[] args)
5650
c1.start();
5751
c2.start();
5852
}
59-
}
53+
}

0 commit comments

Comments
 (0)