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...
In this assignment, you will learn about team building and why it is an important skill...
In this assignment, you will learn about team building and why it is an important skill for managers and leaders. Identify two groups you belong to or have belonged to; the groups can be personal or work-related. Based on your observations of how the groups function and the interactions among the groups' members, answers to the following questions: What was the composition of each group? What types of groups were they? Were the values and mission of the group apparent?...
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...
The main purpose of this individual assignment is to learn about Cultural Competence to increase professionalism...
The main purpose of this individual assignment is to learn about Cultural Competence to increase professionalism and become more job ready in the multicultural workforce. For this individual assignment, please do the following: Complete Cultural Competence Self-Assessment from the course Reading Package. Identify strongest areas of your cultural awareness, knowledge, and skills (two for each section). Identify weakest areas of your cultural awareness, knowledge, and skills (one for each section). List specific steps you will take toward developing your cultural...
Project objective: To learn more about OS scheduling through a hands-on simulation programming experience Implement the...
Project objective: To learn more about OS scheduling through a hands-on simulation programming experience Implement the following 3 CPU scheduling algorithms Simulate and evaluate each with the set of eight processes below. Use any programming language. The program listing should be submitted with the report. FCFS non-preemptive (partial results provided) SJF non-preemptive MLFQ Multilevel Feedback Queue (absolute priority in higher queues)             Queue 1 uses RR scheduling with Tq = 5             Queue 2 uses RR scheduling with Tq =...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT