In: Computer Science
Objective: The objective of this assignment is to learn about how to use common concurrency mechanism.
Task: In this assignment you will be doing the followings:
1. Write a program (either using Java, python, C, or your choice of programming language) to create three threads: one for deposit function and two for withdraw functions.
2. Compile and run the program without implementing any concurrency mechanism. Attach your output snapshot.
3. Now, modify your program to implement any concurrency mechanism. Compile and run. Attach your output snapshot.
Submission:
Submit your program with the output screenshots and explain your observations.
PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU
NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU
AS I DONE MOST OF YOUR ANSWERS, THOUGH WE ARE ONLY ALLOWED TO
ATTEMPT ONE ANSWER OR FOUR SUB PARTS, PLEASE GIVE IT A THUMBS
UP
1. Write a program (either using Java, python, C, or your choice of programming language) to create three threads: one for deposit function and two for withdraw functions.
class Account {
public int balance;
public int accountNo;
void displayBalance() {
System.out.println("Account No:" + accountNo + "Balance: " + balance);
}
void deposit(int amount) {
balance = balance + amount;
System.out.println(amount + " is deposited");
displayBalance();
}
void withdraw(int amount) {
balance = balance - amount;
System.out.println(amount + " is withdrawn");
displayBalance();
}
}
class TransactionDeposit implements Runnable {
int amount;
Account accountX;
TransactionDeposit(Account x, int amount) {
accountX = x;
this.amount = amount;
new Thread(this).start();
}
public void run() {
accountX.deposit(amount);
}
}
class TransactionWithdraw implements Runnable {
int amount;
Account accountY;
TransactionWithdraw(Account y, int amount) {
accountY = y;
this.amount = amount;
new Thread(this).start();
}
public void run() {
accountY.withdraw(amount);
}
}
class TransactionWithdraw2 implements Runnable {
int amount;
Account accountY;
TransactionWithdraw2(Account y, int amount) {
accountY = y;
this.amount = amount;
new Thread(this).start();
}
public void run() {
accountY.withdraw(amount);
}
}
class Bank_concurrency {
public static void main(String args[]) {
Account ABC = new Account();
ABC.balance = 1000;
ABC.accountNo = 111;
TransactionDeposit t1;
TransactionWithdraw t2;
TransactionWithdraw2 t3;
t1 = new TransactionDeposit(ABC, 500);
t2 = new TransactionWithdraw(ABC, 900);
t3 = new TransactionWithdraw2(ABC, 500);
}
}
2. Compile and run the program without implementing any concurrency mechanism. Attach your output snapshot.
3. Now, modify your program to implement any concurrency mechanism. Compile and run. Attach your output snapshot.
/* Java program shows Account Balance using Concurrency Mechanism */class Account {
public int balance;
public int accountNo;
void displayBalance() {
System.out.println("Account No:" + accountNo + "Balance: " + balance);
}
synchronized void deposit(int amount) {
balance = balance + amount;
System.out.println(amount + " is deposited");
displayBalance();
}
synchronized void withdraw(int amount) {
balance = balance - amount;
System.out.println(amount + " is withdrawn");
displayBalance();
}
}
class TransactionDeposit implements Runnable {
int amount;
Account accountX;
TransactionDeposit(Account x, int amount) {
accountX = x;
this.amount = amount;
new Thread(this).start();
}
public void run() {
accountX.deposit(amount);
}
}
class TransactionWithdraw implements Runnable {
int amount;
Account accountY;
TransactionWithdraw(Account y, int amount) {
accountY = y;
this.amount = amount;
new Thread(this).start();
}
public void run() {
accountY.withdraw(amount);
}
}
class TransactionWithdraw2 implements Runnable {
int amount;
Account accountY;
TransactionWithdraw2(Account y, int amount) {
accountY = y;
this.amount = amount;
new Thread(this).start();
}
public void run() {
accountY.withdraw(amount);
}
}
class Bank_concurrency {
public static void main(String args[]) {
Account ABC = new Account();
ABC.balance = 1000;
ABC.accountNo = 111;
TransactionDeposit t1;
TransactionWithdraw t2;
TransactionWithdraw2 t3;
t1 = new TransactionDeposit(ABC, 500);
t2 = new TransactionWithdraw(ABC, 900);
t3 = new TransactionWithdraw2(ABC, 500);
}
}