Repository navigation
Expand file tree
/
Copy pathWithdrawThread.java
More file actions
78 lines (59 loc) · 1.46 KB
/
Copy pathWithdrawThread.java
File metadata and controls
78 lines (59 loc) · 1.46 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package com.cybage.Thread;
class InsufficientBalanceException extends Exception {
public InsufficientBalanceException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
}
class BankAccount{
private static double balance;
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public void withdraw(double amount)
{
try {
if(amount > balance )
{
throw new InsufficientBalanceException("Insufficient Balance");
}
balance = balance - amount;
System.out.println("Withdraw Successfull .... your balance is : " + balance);
}
catch(InsufficientBalanceException e)
{
System.out.println(e.getMessage());
}
}
}
class Member extends Thread{
double amount;
BankAccount bankAccount;
public Member(BankAccount bankAccount , double amount) {
super();
this.bankAccount = bankAccount;
this.amount = amount;
}
@Override
public void run() {
// TODO Auto-generated method stub
synchronized(bankAccount)
{
bankAccount.withdraw(amount);
}
}
}
public class WithdrawThread {
public static void main(String[] args) {
// TODO Auto-generated method stub
BankAccount account = new BankAccount();
account.setBalance(5000);
Member member = new Member(account , 3000);
Member member1 = new Member(account , 3000);
member.start();
member1.start();
}
}