-
Notifications
You must be signed in to change notification settings - Fork 243
/
banking.java
54 lines (50 loc) · 1.24 KB
/
banking.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
class Customer {
int amount=10000;
synchronized void withdraw(int amount) {
System.out.println("Going to Withdraw...");
if(this.amount<amount) {
System.out.println("Going to Withdraw..");
try {
wait();
}
catch(Exception e) {
System.out.println(e);
}
}
this.amount-=amount;
System.out.println("Withdraw completed");
}
synchronized void deposit(int amount) {
System.out.println("Going to deposit");
this.amount+=amount;
System.out.println("Deposit completed..");
notify();
}
}
class MyThread1 extends Thread {
Customer c;
MyThread1(Customer c) {
this.c=c;
}
public void run() {
c.withdraw(15000);
}
}
class MyThread2 extends Thread {
Customer c;
MyThread2(Customer c) {
this.c=c;
}
public void run() {
c.deposit(15000);
}
}
public class banking {
public static void main(String args[]) {
Customer c= new Customer();
MyThread1 t1=new MyThread1(c);
MyThread2 t2=new MyThread2(c);
t1.start();
t2.start();
}
}