forked from Gaurang2908/HacktoberFest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BankWithDraw.java
59 lines (59 loc) · 1.06 KB
/
BankWithDraw.java
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
59
class Bank
{
float balance=5000.00f;
synchronized public void withdraw(float amt)
{
System.out.println("Customer started to withdraw money");
try {
if(balance<amt)
{
System.out.println("Less Balance,Waiting for Deposit");
wait();
}
balance -=amt;
System.out.println("Withdraw completed");
} catch (Exception e) {
System.out.println(e);
}
}
synchronized public void deposit(float amt)
{
System.out.println("Customer started to Deposit Money");
balance +=amt;
System.out.println("Deposit Completed");
notify();
}
}
class Customer1 extends Thread
{
Bank b;
Customer1(Bank b)
{
this.b=b;
}
public void run()
{
b.withdraw(5000.0f);
}
}
class Customer2 extends Thread
{
Bank b;
Customer2(Bank b)
{
this.b=b;
}
public void run()
{
b.deposit(5000.00f);
}
}
public class BankWithDrawExample {
public static void main(String[] args) {
Bank b=new Bank();
Customer1 c1=new Customer1(b);
c1.start();
Customer2 c2=new Customer2(b);
c2.start();
}
}