-
Notifications
You must be signed in to change notification settings - Fork 0
/
code16.java
112 lines (95 loc) · 2.13 KB
/
code16.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import java.lang.Thread;
// //Bank System//
// class Bank{
// int amount = 500;
// synchronized public void withdraw(int money){
// if(money>amount){
// try{
// wait();
// }
// catch(Exception e){
// System.out.println(e);
// }
// }
// amount = amount-money;
// System.out.println("amount after withraw"+amount);
// }
// synchronized public void deposit(int money){
// amount = amount + money;
// notify();
// System.out.println("amount after deposite"+amount);
// }
// }
// class A1 extends Thread{
// Bank b1;
// A1(Bank b){
// b1=b;
// }
// public void run(){
// b1.withdraw(1000);
// }
// }
// class D1 extends Thread{
// Bank b2;
// D1(Bank b){
// b2=b;
// }
// public void run(){
// b2.deposit(600);
// }
// }
// public class code16 {
// public static void main(String[] args) {
// Bank obj = new Bank();
// A1 obj1 = new A1(obj);
// D1 obj2 = new D1(obj);
// obj1.start();
// obj2.start();
// }
// }
class Customer{
int amount = 500;
synchronized public void withdraw(int money){
if(amount<money){
try{
wait();
}catch(InterruptedException e ){
System.out.println(e);
}
amount = amount - money;
System.out.println(amount);
}
}
synchronized public void deposit(int money){
amount = amount+money;
System.out.println(amount);
notify();
}
}
class A extends Thread{
Customer c1;
A(Customer c){
c1= c;
}
public void run(){
c1.withdraw(1000);
}
}
class B extends Thread{
Customer c2;
B(Customer c){
c2= c;
}
public void run(){
c2.deposit(1000);
}
}
public class code16{
public static void main(String[] args) {
Customer obj = new Customer();
A obj1 = new A(obj);
B obj2 = new B(obj);
obj1.start();
obj2.start();
}
}