Demonstrate a simple use for synchronization in Java. Create a bank account clas
ID: 3772182 • Letter: D
Question
Demonstrate a simple use for synchronization in Java.
Create a bank account class called Account using Java with methods deposit & withdraw. The deposit method should accept attribute amount & update balance to the sum of amount & balance. Similarly, the withdraw method should accept the attribute amount & update the balance ‘balance – amount’ if balance > = amount or print an error otherwise. Below is the stub.
public class Account {
private double balance = 0;
public Account(double balance) {
this.balance = balance;
}
public void deposit(double amount) {
//Implementation here
}
public void withdraw(double amount) {
//Implementation here
}
}
Create 2 thread classes (1 for withdrawal and 1 for deposit). The stub is below:
public class WithdrawThread implements Runnable {
private Account account;
private double amount;
public WithdrawThread(Account account, double amount) {
//Set the account & balance
}
public void run() {
//make a withdrawal
}
}//end WithdrawThread class
public class DepositThread implements Runnable {
private Account account;
private double amount;
public DepositThread(Account account, double amount) {
//Set the account & balance
}
public void run() {
//make a deposit
}}
Create a main class below and run it. Provide output.
public class InternetBankingSystem {
public static void main(String [] args ) {
Account accountObject = new Account(100);
new Thread(new DepositThread(accountObject,30)).start();
new Thread(new DepositThread(accountObject,20)).start();
new Thread(new DepositThread(accountObject,10)).start();
new Thread(new WithdrawThread(accountObject,30)).start();
new Thread(new WithdrawThread(accountObject,50)).start();
new Thread(new WithdrawThread(accountObject,20)).start();
} // end main()
}
Now, update the account class using synchronization of methods, to ensure that the balance is locked during the withdrawal & deposit methods. Run the main class again and provide output.
Explanation / Answer
Account.java
public class Account {
private double balance=0;
public Account(double balance)
{
this.balance = balance;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
}
WithdrawThread.java
public class WithdrawThread implements Runnable
{
private Account account;
private double amount;
public WithdrawThread(Account account, double amount)
{
// Set the account & balance
this.account = account;
this.amount = amount;
}
public void withdraw(double amount) throws InterruptedException {
double bal = account.getBalance();
if (amount > bal)
{
wait();
throw new IllegalArgumentException("wrong amount");
}
bal-= amount;
account.setBalance(bal);
notifyAll();
System.out.println("withdraw " + amount + " new balance in thread number"
+ Thread.currentThread().getId() + " balance is " + bal);
}
public synchronized void run() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// make a withdrawal
try {
withdraw(amount);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}// end WithdrawThread class
DepositThread.java
public class DepositThread implements Runnable {
private Account account;
private double amount;
public DepositThread(Account account, double amount) {
// Set the account & balance
this.account = account;
this.amount = amount;
}
public void deposit(double amount) throws InterruptedException {
double bal = account.getBalance();
if (amount <= 0){
wait();
throw new IllegalArgumentException("Can't not deposit");
}
bal += amount;
account.setBalance(bal);
System.out.println("Deposit "+ amount + " new balance in thread number"
+ Thread.currentThread().getId() + " balance is " + bal);
}
public synchronized void run() {
// make a deposit
try {
deposit(amount);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
InternetBankingSystem.java
public class InternetBankingSystem {
public static void main(String[] args) {
Account accountObject = new Account(200);
new Thread(new DepositThread(accountObject,30)).start();
new Thread(new WithdrawThread(accountObject, 50)).start();
new Thread(new DepositThread(accountObject,10)).start();
new Thread(new WithdrawThread(accountObject, 20)).start();
new Thread(new WithdrawThread(accountObject, 30)).start();
new Thread(new DepositThread(accountObject,20)).start();
} // end main()
}
Sample output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.