Note: Thread no 1 = Owner 1 -> perform 10 transactions
Thread no 2 = Owner 2 -> perform 10 transactions
Problem: in Output:
Owner: 1 transaction: 2
Owner: 1 transaction: 1
credit: $46 credit: $30 balance: $146
balance: $130
After Transaction 2 the balance should be $176. There must be some problem in thread pooling. I tried a lot but could not reach the desired solution. Any help would be appreciable.
Output
run:
Initial Balance: $100
Owner: 1 transaction: 1
Owner: 1 transaction: 2
credit: $46 credit: $30 balance: $146
balance: $130
Owner: 2 transaction: 1
Owner: 2 transaction: 2
debit: $46 debit: $30 Debit Done Debit Done Balance: $100 Balance: $100
Owner: 1 transaction: 3
credit: $75 Owner: 1 transaction: 4
balance: $175
credit: $47 Owner: 2 transaction: 3
balance: $147
Owner: 2 transaction: 4
debit: $47 debit: $75 Debit Done Debit Done Balance: $100 Balance: $100
Owner: 1 transaction: 5
credit: $57 Owner: 1 transaction: 6
balance: $157
credit: $13 Owner: 2 transaction: 5
balance: $113
debit: $57 Debit Done Owner: 2 transaction: 6
Balance: $100 debit: $13 Debit Done Balance: $100 Owner: 1 transaction: 7
credit: $84 Owner: 1 transaction: 8
balance: $184
credit: $95 Owner: 2 transaction: 7
balance: $195
Owner: 2 transaction: 8
debit: $84 Debit Done debit: $95 Debit Done Balance: $100 Balance: $100
Owner: 1 transaction: 10 credit: $96 balance: $196
Owner: 1 transaction: 9
Owner: 2 transaction: 10
credit: $20 debit: $96 Debit Done balance: $120
Balance: $100
Owner: 2 transaction: 9
debit: $20 Debit Done Balance: $100B
Bank
public class Bank{
private BankAccount bankAccount = new BankAccount(100);
public void displayInitialBalance(){
System.out.println("Initial Balance:"+getBankAccount());
}
/**
* @return the bankAccount
*/
public BankAccount getBankAccount() {
return bankAccount;
}
/**
* @param bankAccount the bankAccount to set
*/
public void setBankAccount(BankAccount bankAccount) {
this.bankAccount = bankAccount;
}
public static void main(String[] args){
BankAccount bankAccount = new BankAccount(100);
System.out.println("Initial Balance: $"+bankAccount.getCurrentBalance());
ExecutorService executorService = Executors.newFixedThreadPool(2);
for(int count = 1; count<=10; count++){
Runnable runnable = new Owner(count);
executorService.execute(runnable);
}
executorService.shutdown();
while(!executorService.isTerminated()){
}
System.out.println("All Transactions Done");
}
}
BankAccount
public class BankAccount {
private int currentBalance;
public BankAccount(int initialBalance){
this.currentBalance = initialBalance;
}
synchronized public void credit(int amount){
setCurrentBalance(currentBalance + amount);
System.out.print("credit: $"+amount+"\t");
System.out.println(" balance: $"+getCurrentBalance());
}
synchronized public void debit(int amount){
if(getCurrentBalance() >= amount){
setCurrentBalance(currentBalance - amount);
System.out.print(" debit: $"+amount);
System.out.print(" Debit Done" +"\t");
System.out.print(" Balance: $"+getCurrentBalance());
}
else{
System.out.print("Insufficient Balance"+"\t");
System.out.println("Balance: $"+getCurrentBalance());
}
}
/**
* @return the currentBalance
*/
public int getCurrentBalance() {
return currentBalance;
}
/**
* @param currentBalance the currentBalance to set
*/
public void setCurrentBalance(int currentBalance) {
this.currentBalance = currentBalance;
}
}
Owner
public class Owner implements Runnable{
public int transactionCount = 0;
public int amount = 0;
private BankAccount bankAccount = new BankAccount(100);
public Owner(int transactionCycle){
this.transactionCount = transactionCycle;
}
public Owner(int transactionId, BankAccount bankAccount){
this.bankAccount = bankAccount;
this.transactionCount = transactionId;
}
@Override
public void run() {
try{
amount = (int)(Math.random() * 100 + 10);
Thread call = new Thread();
call.start();
Thread.currentThread().setName("Owner: 1 "+" transaction: "+transactionCount);
(transactionCount)++;
System.out.println(Thread.currentThread().getName());
getBankAccount().credit(amount);
Thread.currentThread().setName("Owner: 2 "+" transaction: "+ --(transactionCount));
(transactionCount)++;
System.out.println(Thread.currentThread().getName());
getBankAccount().debit(amount);
}catch(UnsupportedOperationException e){
e.printStackTrace();
}
}
/**
* @return the bankAccount
*/
public BankAccount getBankAccount() {
return bankAccount;
}
/**
* @param bankAccount the bankAccount to set
*/
public void setBankAccount(BankAccount bankAccount) {
this.bankAccount = bankAccount;
}
}