Question

In: Computer Science

Objective: The objective of this assignment is to learn about how to use common concurrency mechanism.        ...

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.

Solutions

Expert Solution

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);
  }
}

Related Solutions

Objective To learn more about the chemistry of transition and inner transition metals by using the...
Objective To learn more about the chemistry of transition and inner transition metals by using the Internet. Be able to use your knowledge and understanding of transition metal chemistry to contribute to the discussion board. Background In the first and second semesters of general chemistry, there is a great deal of discussion around the Group 1A and Group 2A metals. The discussion regarding the transition and inner transition metals is much less. Transition metals and their compounds display a variety...
Common Sense Media surveyed 1000 teens and 1000 parents of teens to learn about how teens...
Common Sense Media surveyed 1000 teens and 1000 parents of teens to learn about how teens are using social networking sites such as Facebook and Instagram. The two samples were independently selected. When asked if they check their online social networking sites more than 10 times a day, 220 of the teens surveyed said yes. When parents of teens were asked if their teen checked his or her site more than 10 times a day, 40 said yes. Conduct a...
The purpose of this assignment is to learn how to apply the t-test to a sample...
The purpose of this assignment is to learn how to apply the t-test to a sample dataset. For this assignment, students will use IBM SPSS Statistics and the "Example Dataset." Using the "Example Dataset" and SPSS, apply the t-test to assess the following statement: "Men and women have different incomes in this city." Show your calculations and copy of the SPSS output in a Word document. In a separate 250-500 Word document, address the following questions: Describe what t-test is...
In this assignment, you will learn how to describe the distribution of the sample mean (),...
In this assignment, you will learn how to describe the distribution of the sample mean (), and investigate how sample size affects variability of the sample mean (). To find out various probability values you will have to calculate the standard error of, and use the standard error to convert into Z score. MARS Chocolate claims* that the net weight of its 31 oz Party Bucket is normally distributed with a mean of 32 oz and a standard deviation of...
Locking methods are one of the most common techniques used in concurrency control. There are different...
Locking methods are one of the most common techniques used in concurrency control. There are different levels of lock use. Make sure you clearly write down (a), (b) followed by your answers. [6 marks] (a) Use the following scenario to illustrate and explain how a table-level lock and a page-level lock work. Scenario: Transaction 1 (T1) wants to update row 5 in Table A Transaction 2 (T2) wants to update row 2 in Table A Note: Page 1 contains rows...
For this assignment, you are to find a quality, resource on how to learn more effectlvely...
For this assignment, you are to find a quality, resource on how to learn more effectlvely and efficiently. One could target this tower expertise (see Bloom's. https://www.legacee.com/wp-content/uploads/2014/03/Blooms-Taxonomy.pdf (Links to an external site.)Links to an external site. Or it could be more oriented toward a theory or a method of developing a skill. One can https://www.legacee.com/learning-new-skills/practical-theory/ (Links to an external site.)Links to an external site. Suggest something practical — by that I mean something that you can actually practice or is...
Neurotransmitters paper URGENT Please Objectives of the Assignment Research and learn about the different classes of...
Neurotransmitters paper URGENT Please Objectives of the Assignment Research and learn about the different classes of NTs and Receptors Search for credible information on the web and your textbook Practice research techniques (information literacy) and source citation Start with an introductory paragraph telling the reader what to expect in your paper First, explain the specific release of NT used at the NMJ, the class of receptor it binds to, and how that specific NT is removed from the synaptic cleft...
Consider that you want to learn more about how people use USB portable storage, as compared...
Consider that you want to learn more about how people use USB portable storage, as compared to network/cloud-based storage (in web accounts) or storage on their computer hard drives. More specifically, you want to learn how children and young adults (aged 10–18) and older users (aged 65–85) use these storage devices. You want to learn more about which devices individuals prefer and in what situations they use them. Would you use probabilistic or non-probabilistic sampling? (Provide explanation for sampling approach)...
This assignment is about using attributes in HTML. The attributes you will use for this assignment...
This assignment is about using attributes in HTML. The attributes you will use for this assignment include the lang attribute, the type attribute, and the start attribute. For this assignment, you will write an HTML document to create a web page that contains ordered lists with numbers, uppercase letters, lowercase letters, uppercase Roman numerals, and lowercase Roman numerals; and a list with lowercase Roman numerals that starts at a value other than 1. Your document can be about any topic...
To learn a language you need: A. Innate mechanism, motivation, and cognitive ability. B. Innate mechanism,...
To learn a language you need: A. Innate mechanism, motivation, and cognitive ability. B. Innate mechanism, ability to mimic, and cognitive ability. C. Innate mechanism, motivation, and the right environment.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT