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

Part 1Title: Computer Hardware Using bullet points, briefly describe a computing device you use often, such as your desktop, laptop, tablet, or smartphone. Identify the make and model, and at least th

Part 1Title: Computer Hardware Using bullet points, briefly describe a computing device you use often, such as your desktop, laptop, tablet, or smartphone. Identify the make and model, and at least three other components of the computing device. In the speaker notes, discuss in detail what you use the device

Network Threats and Attacks You have now learned about recognizing security threats, vulnerabilities, and how to mitigate them. Almost every day, there are reports of an intrusion, breach of confident

Network Threats and Attacks You have now learned about recognizing security threats, vulnerabilities, and how to mitigate them. Almost every day, there are reports of an intrusion, breach of confidentiality, or some other high-profile attack. Find a news article about a recent network attack (no more than one year ago).

I am doing IFSM 300 Information Systems in Organization. the instructions The Stage 4 case study project is due as shown in the schedule. The assignment instructions and vendor brochure are attached.

I am doing IFSM 300 Information Systems in Organization. the instructions The Stage 4 case study project is due as shown in the schedule. The assignment instructions and vendor brochure are attached. The Case Study is posted under Course Resources. Use the Case Study and weekly readings to develop your assignment and understand the concepts

Find 5 student organizations that interest you. For each organization: Why are you interested in this organization?Describe some of the activities of the organizationWhen and where is the next meeti

Find 5 student organizations that interest you.  For each organization: Why are you interested in this organization? Describe some of the activities of the organization When and where  is the next meeting? If you have questions about the organization, how would you contact them?CSE1106 – Intro to Computer Science and

Network Threats and Attacks You have now learned about recognizing security threats, vulnerabilities, and how to mitigate them. Almost every day, there are reports of an intrusion, breach of confident

Network Threats and Attacks You have now learned about recognizing security threats, vulnerabilities, and how to mitigate them. Almost every day, there are reports of an intrusion, breach of confidentiality, or some other high-profile attack. Find a news article about a recent network attack (no more than one year ago).

Linux in Action (Required/Graded) Review this Linux case study. In particular, review the section titled “Results.” Share your thoughts on Linux after reviewing this study. Did you learn anything tha

Linux in Action (Required/Graded)  Review this Linux case study. In particular, review the section titled “Results.” Share your thoughts on Linux after reviewing this study. Did you learn anything that surprised you? Did it influence your opinion about Linux? Why or why not? In addition to your primary forum response, post

Linux Project and Virtualization (Required/Graded) Next week, the final element of your project will be due. In this phase, you’ll install Linux into a virtual machine and demonstrate some Linux comm

Linux Project and Virtualization (Required/Graded)  Next week, the final element of your project will be due. In this phase, you’ll install Linux into a virtual machine and demonstrate some Linux commands. A vital element of the project is virtualization. Have you previously used virtualization tools, such as VMware, VirtualBox, HyperV,

Careers in Linux System Administration (Required/Graded) SubscribeChoose from one of the following options. Be sure to include the option number in your response. 1. Job Search Use Indeed.com, Monst

Careers in Linux System Administration (Required/Graded) SubscribeChoose from one of the following options. Be sure to include the option number in your response. 1. Job Search Use Indeed.com, Monster.com, or LinkedIn.com to research IT job careers that require Linux Administration skills in your area. You can use keywords such as "Linux

Write a Java program as follows:1. Prompt the user on which action they want to take:a. Convert cubic feet to U.S. bushelsb. Convert miles to kilometersc. Determine graduation with honors titled. Exit

 Write a Java program as follows:1. Prompt the user on which action they want to take:a. Convert cubic feet to U.S. bushelsb. Convert miles to kilometersc. Determine graduation with honors titled. Exit program2. Programs at a minimum must have the following methods:a. Convert cubic feet method that gets cubic feet

Calculator Server and Client Java The goal: Make a client that sends simple arithmetic expressions, and a server that solves them and sends the result back to the client. Class design requirements:

Calculator Server and Client  Java  The goal: Make a client that sends simple arithmetic expressions, and a server that solves them and sends the result back to the client.  Class design requirements:  Your program should contain at least the following classes • CalculatorClient • CalculatorServer Important note: This is an

This is about java. I would appreciate your help! Please do not provide a “copy and paste answer”! Among other things, the purpose of this assignment is to assess the student’s ability to write a pro

This is about java. I would appreciate your help! Please do not provide a “copy and paste answer”!  Among other things, the purpose of this assignment is to assess the student’s ability to write a program dealing with window events, mouse events, and the Delegation Event Model. PROGRAM SPECIFICATIONS Beginning

The following program draws an BB-8 as shown below. Modify the program to move the BB-8 left or right using the arrow keys. The program can be download from the CMS. Please use Javafx   import java

The following program draws an BB-8 as shown below.  Modify the program to move the BB-8 left or right using the arrow keys.  The program can be download from the CMS. Please use Javafx  import javafx.application.Application; import javafx.scene.Scene; import javafx.scene.input.KeyCode; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.scene.shape.Line; import javafx.stage.Stage;