Our Services

Get 15% Discount on your First Order

[rank_math_breadcrumb]

Mutex locks and semaphores, as discussed in class, are different techniques to solve the race condition and to ensure an efficient synchronization between cooperating threads and processes. you wil

Mutex  locks and semaphores, as discussed in class, are different techniques to  solve the race condition and to ensure an efficient synchronization  between cooperating threads and processes. you will use semaphores to  solve a number of synchronization problems between cooperating threads.

important to note that:

•  Semaphore, in literature, uses wait() and signal(). However, in the  standard library of Java, these functions are acquire() and release()  respectively. The same functionalities but with different names.

In  the first question, the deposit and withdraw functions share a bank  account to add certain amount or subtract certain amount from the  balance, respectively. 

Use semaphore(s) to implement the synchronization.

Implent the following to the code, 

1. Implement the deposit functionality method

 Deposit the input amount to the balance only if the current balance is less than 2000$

 Deposit  doesn’t wait until this condition is true (If the condition is false,  skip adding the amount), thus use if statements rather than waiting  while loops

 Call the displayStatus() function after you deposit the amount and before release the lock

2. Implement the withdraw functionality, method

 Withdraw the input amount from the balance only if the current balance is greater than or equal to input amount

 Withdraw  doesn’t wait until this condition is true (if the condition is false,  skip withdrawing the amount), thus use if statements rather than waiting  while  loops

 Call the displayStatus() function after you remove the amount and before release the lock

//Question1_WithdrawDeposit.java file

package Threads_Synchronization;

import java.util.concurrent.Semaphore;

import java.util.concurrent.locks.Lock;

import java.util.concurrent.locks.ReentrantLock;

public class Question1_WithdrawDeposit {

/*

* In this question use semaphore(s) to enable process synchronization

*

* Thread 1 and thread 2 (in the main function) share a single bank account (initial balance of 1000$).

* thread 1 can deposit certain input amount to the balance only if the current balance is less than 2000$

*  thread 2 can withdraw certain input amount from the balance only if the  current balance is greater than or equal to the input amount.

*

*/

// shared resources between thread 1 and thread 2 are:

public static int balance = 1000; //the initial value of the account’s balance

//DONOT CHANGE THIS VARIABLE

// add below any further resources you think the deposit and withdraw threads/functions must share

//——————————————–end of shared resources section

// this function simply displays the current balance of the shared account and which thread made the call

// DONOT CHANGE THIS FUNCTION

public static void displayStatus() {

if(Thread.currentThread().getName().equals(“withdraw”))

System.out.println(“The  withdraw function successfully took the amount and the current value of  the account’s balance is :”+ balance + “$”);

else

System.out.println(“The  deposit function successfully added the amount and the current value of  the account’s balance is :”+ balance + “$”);

}

// this function accepts an input integer amount value to deposit into the shared account

public static void Deposit(int amount){

try {

System.out.println(“The deposit function is trying to add “+ amount +”$ to the shared balance”+ balance + “$”);

//IMPLEMENT HERE the deposit functionality,

// Deposit the input amount to the balance only if the current balance is less than 2000$

//  Deposit doesn’t wait until this condition is true (If the condition is  false, skip adding the amount), thus use if statements rather than  waiting while loops

// Call the displayStatus() function after you deposit the amount and before release the lock

// Implement the deposit functionality, as detailed above, in the area below

//——————————————–end of Deposit function

} catch (Exception e) {

System.out.println(“Problem with the deposite function “+e.toString());

}

}

// this function accepts an input integer amount value to withdraw from the shared account

public static void Withdraw(int amount){

try {

System.out.println(“The withdraw is trying to remove “+ amount +”$ from the shared balance”+ balance + “$”);

//IMPLEMENT HERE the withdraw functionality,

// withdraw the input amount from the balance only if the current balance is greater than or equal to input amount

//  Withdraw doesn’t wait until this condition is true (if the condition is  false, skip withdrawing the amount), thus use if statements rather than  waiting while loops

// Call the displayStatus() function after you remove the amount and before release the lock

// Implement the withdraw functionality, as detailed above, in the area below

//——————————————–end of Withdraw function

} catch (Exception e) {

System.out.println(“Problem with the withdraw function “+e.toString());

}

}

// this is the main function

// DONOT CHANGE THIS SECTION

public static void main(String[] args) {

//create thread 1 to run function 1

Thread thread1 = new Thread(new Runnable() {

@Override

public void run() {

while(true) {

try {

Deposit(200 + (int)(Math.random() * 1000)); //random value between 200 and 1000$

Thread.sleep(200 + (int)(Math.random() * 500)); //random delay between 200 and 500

} catch (Exception e) {

System.out.println(“Problem with thread 1 “+e.toString());

}

}

}

});

//create thread 2 to run function 2

Thread thread2 = new Thread(new Runnable() {

@Override

public void run() {

while(true) {

try {

Withdraw(200 + (int)(Math.random() * 1000)); //random value between 200 and 1000$

Thread.sleep(200 + (int)(Math.random() * 500)); //random delay between 200 and 500

} catch (Exception e) {

System.out.println(“Problem with thread 2 “+e.toString());

}

}

}

});

//ask the threads to start running

thread1.setName(“deposit”);

thread1.start();

thread2.setName(“withdraw”);

thread2.start();

}

}

Share This Post

Email
WhatsApp
Facebook
Twitter
LinkedIn
Pinterest
Reddit

Order a Similar Paper and get 15% Discount on your First Order

Related Questions

In Units V, VI, and VII, you learned about the components of a computer, how a computer works, the internet, networks and network communications, cloud computing, web development, digital identity, so

In Units V, VI, and VII, you learned about the components of a computer, how a computer works, the internet, networks and network communications, cloud computing, web development, digital identity, social media, e-commerce, ethical behavior, databases, and explored two Microsoft Office applications, PowerPoint and Access.  In this assignment, you will

In the previous assignment, the annotated bibliography, you collected 15 – 20 references. Now, you are to craft an analytical research paper. Using the same themes assigned for the previous assignment

In the previous assignment, the annotated bibliography, you collected 15 – 20 references. Now, you are to craft an analytical research paper. Using the same themes assigned for the previous assignment.  GROUP 1: Context-Aware Computing  Prepare a ten (10) page research paper. In the paper, you are to cover the

This week, you will submit the second project, the Desktop Migration Proposal. Using the requirements analysis your manager provided and the Internet research you conducted, submit your recommendation

This week, you will submit the second project, the Desktop Migration Proposal. Using the requirements analysis your manager provided and the Internet research you conducted, submit your recommendation to the assignment folder. As you are writing your recommendation, ensure your analysis and recommendations align with your manager’s priorities and concerns.

In Units I, II, and III, you learned about the history of computers, application and system software, blockchain, cryptocurrency, computer ethics, and explored two Microsoft Office applications, Word

In Units I, II, and III, you learned about the history of computers, application and system software, blockchain, cryptocurrency, computer ethics, and explored two Microsoft Office applications, Word and Excel.  In this assignment you will demonstrate what you have learned in these three units. This assignment will have five parts. 

Research and brainstorm three (3) proposals for a mobile computing solution. Write up a half-page (double-spaced, approx. 250 words) description of each mobile computing solution proposal. Provide an

Research and brainstorm three (3) proposals for a mobile computing solution.  Write up a half-page (double-spaced, approx. 250 words) description of each mobile computing solution proposal. Provide answers to the following questions: 1) What is the 1 sentence summary of the mobile computing solution? 2) Who and how big is

Creating and Submitting your Portfolio Overview Creating a professional cybersecurity portfolio is essential for showcasing your skills, achievements, and growth. This portfolio will serve as a valuab

Creating and Submitting your Portfolio Overview Creating a professional cybersecurity portfolio is essential for showcasing your skills, achievements, and growth. This portfolio will serve as a valuable tool for presenting yourself to universities, scholarship committees, employers, and beyond. Objective:: To design a professional cybersecurity portfolio that markets your academic and

IntroductionFor this project, you will create a personal portfolio website to demonstrate your ability to use HTML, CSS, and JavaScript and resolve software problems in web development environments. Y

IntroductionFor this project, you will create a personal portfolio website to demonstrate your ability to use HTML, CSS, and JavaScript and resolve software problems in web development environments. You will create three HTML pages: a résumé, a cover letter, and a career goals page. As you make your website, you

IntroductionFor this project, you will create a personal portfolio website to demonstrate your ability to use HTML, CSS, and JavaScript and resolve software problems in web development environments. Y

IntroductionFor this project, you will create a personal portfolio website to demonstrate your ability to use HTML, CSS, and JavaScript and resolve software problems in web development environments. You will create three HTML pages: a résumé, a cover letter, and a career goals page. As you make your website, you

Need to report 1 You will submit two reports generated from your developed database. These reports should be meaningful to your database. The report(s) resolve one of the problems that you originally

Need to report 1 You will submit two reports generated from your developed database. These reports should be meaningful to your database. The report(s) resolve one of the problems that you originally identified in your Database Proposal.  The lay out should be appealing to the eye and the design should

Select any example visualization or infographic and imagine the contextual factors have changed: If the selected project was a static work, what ideas do you have for potentially making it usefully in

Select any example visualization or infographic and imagine the contextual factors have changed: If the selected project was a static work, what ideas do you have for potentially making it usefully interactive? How might you approach the design if it had to work on both mobile/tablet and desktop? If the

Excel 365/2021 Capstone – Level 2 Working with Sales Data These instructions are compatible with both Microsoft Windows and Mac operating systems. In this project, you will work with sales data from T

Excel 365/2021 Capstone – Level 2 Working with Sales Data These instructions are compatible with both Microsoft Windows and Mac operating systems. In this project, you will work with sales data from Top’t Corn, a popcorn company with an online store, multiple food trucks, and two retail stores. You will

Hello Students, As discussed in class, the Workspaces and Challenges are now active. Required Challenges for Full Credit To earn full credit (30% of the assignment), you must complete 6 challenges fro

Hello Students, As discussed in class, the Workspaces and Challenges are now active. Required Challenges for Full Credit To earn full credit (30% of the assignment), you must complete 8 challenges from the following list: Interns & HR on the Domain Controller Help Desk Fun: User Workstation Nightmares Dangerous Drives Preventative Protection: Thwarting the Imminent Threat Security