Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Concurrency Aim: Threads often coordinate their actions using guarded block idiom. In this assignment, you learn polling a condition that must be true before the
Concurrency Aim: Threads often coordinate their actions using guarded block idiom. In this assignment, you learn polling a condition that must be true before the block can proceed. There are several steps to follow to do this correctly. Study this section before solving the problem. Introduction: Use guarded blocks to create a Producer-Consumer application. This kind of application shares data between two threads: the producer, that creates the data, and the consumer, that does something with it. The two threads communicate using a shared object. Coordination is essential: the consumer thread must not attempt to retrieve the data before the producer thread has delivered it, and the producer thread must not attempt to deliver new data if the consumer has not retrieved the old data. Scenario: A restaurant depends on a delivery service to deliver meals to its customers. The restaurant prepares and packages an order of many items and notifies the delivery service by WhatsApp message "READY and waits until the delivery service picks-up the meal and delivers it to the customer. The delivery service takes time to deliver any order depending on traffic, weather conditions, status of their vehicles, etc. Follow instructions to complete the following classes that simulates the activity of the restaurant and the delivery service on a normal working day. class Window ! private String message; private boolean empty = true; // Delivery services picksup a meal from the Window // Write a synchronized pickup method that returns a string as follows: - waits while "empty" is true - when "empty" is false, it sets "empty" to ture and notifies all delivery drivers that a meal is ready returns "message". public synchronized String pickup() { // Restaurant puts a ready meal in the Window // write a synchronized put method as follows: - waits while "empty" is true // - Set empty to false - accept "message" value - notify all parties public synchronized void put (String message) { // ) import java.util. Random; public class DeliveryService implements Runnable ! private Window window; public DeliveryService (Window window) { this.window - window; 1 public void run() { Random random - new Random(); for (String ms - window.pickup(); ! ms.equals("Done"); ms = window.pickup() { // TODO: Print the message formatted as shown in the run. // TODO: Sleep 3 seconds import java.util. Random; public class Restaurant implements Runnable { private Window window; public Restaurant (Window window) { this.window = window; public void run() { String meals[] = { "First Meal", "Second Meal "Third Meal", "Fourth Meal" "Fifth Meal" }; Random random = new Random(); for (int i = 0; i savac sava repo> java Simulate We have delivered: First Meal We have delivered: Second Meal We have delivered: Third Meal We have delivered: Fourth Meal We have delivered: Fifth Meal repo> Your work should be submitted as a Git repository. Every method done should be documented using Git commands
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started