Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

CSE3OSA Assignment

Due 11:59 PM Friday September 30, 2022

Objective

To gain practical experience with multi-threaded concurrency using Java, using a thread-safe shared object to communicate between concurrently executing threads without introducing deadlocks or     errors in the logic of the program.

Submission details

Submit your completed assignment source files (.java files only) via LMS. Please submit separate copies of your source files for each part of the assignment in separate .zip archives---do not just submit one copy of your files for the entire assignment.

This is an individual assignment. You are not permitted to work as part of a group or obtain help from anyone else (online or otherwise) when completing this assignment.

Plagiarism is the submission of somebody else’s work in a manner that gives the impression that the work is your own. La Trobe University treats plagiarism very seriously and harsh penalties will be  imposed on students who plagiarise.

Penalties are applied to late submissions, and there is a hard cut-off 5 working days after the due   date. Please refer to the La Trobe website for more details. To avoid incurring a late penalty due to technical difficulties you should aim to submit at least one day before the due date.

Implementation details

Your assignment solutions must be implemented using Java threads. Furthermore, your solutions must compile and run on simula5. Each part of the assignment follows from the previous one.

Execution test

A time after the submission date will be arranged for you to be present while your solutions are       executed with our own hidden sample data. During this time you will be asked questions about your code to a) verify that you authored it, and b) assess your understanding of concurrent programming  concepts. Attending the execution test is compulsory to receive any marks for the assignment.  Additionally, you will receive a mark out of 12 (3 per assignment part) based on how you explain   and answer questions about your code. You are encouraged to leave comments in your code to aid    your explanation.

Task overview

“Messy Joe’s”is a contemporary restaurant with a quirky yet charming atmosphere. One of the        main appeals of Messy Joe’s is the unique way in which meals are prepared and and delivered to      customers. The chef continuously prepares meals, sliding them along the counter after they are        cooked. The line of meals sitting on the counter is lovingly referred to as the“meal queue”.             Customers at Messy Joe’s eat by taking a meal from the meal queue, adding the meal’s price to their bill, and consuming it. Once they have finished their meal, a customer can return to the meal queue  and take another. Customers are allowed to repeat this process as many times as they like and will    be billed for the meals that they eat. However, there is one very strict rule at Messy Joe’s: in order    to prevent food from getting cold, a customer can only take a meal from the end of the meal queue   (that is, meals are taken in first in, first out order). Over the course of this assignment you will        simulate Messy Joe’s by implementing a multi-threaded Java program.

Part 1: Cooking meals [10 marks]

In this part of the assignment you will implement a Chef thread which prepares meals using the meal descriptions contained in a meals.txt file.

Two source files have been provided for you, Meal.java and Main1.java:

•   Meal.java defines the Meal class. A Meal object contains the name of the meal, whether or not the meal contains meat, and the price of the meal (as an integer---all meals at Messy    Joe’s cost a whole dollar amount).

•   Main1.java contains the main method.

Additionally a sample input file, meals.txt, has been provided. This file contains a list of meals to be prepared, in order. Each line of the file contains the name of the meal (one word), whether the meal  contains meat (true or false) and the price of the meal in dollars (a whole number). For example, a   $13 calamari dish is represented as“Calamari true 13”.

Currently the main method contains the code for preparing meals. It opens the meals.txt file and      reads the data inside to instantiate Meal objects. But that’s the chef’s job! You shall create a Chef    thread class (in a new file, Chef.java) which runs this code instead. Update the main method to start and join a Chef thread instead of preparing meals directly. Example output:

$ java Main1

Chef

Chef

Chef

Chef

Chef

Chef

Chef

Chef

Chef

prepared

prepared

prepared

prepared

prepared

prepared

prepared

prepared

finished

Calamari

Bruschetta

Wings

Burger

Curry

Gnocchi

Schnitzel

Pumpkin


Submit Main1.java, Meal.java, and Chef.java together as Part_1.zip.


Part 2: Implementing the meal queue [30 marks]

In this part of the assignment you will implement a thread-safe MealQueue object to facilitate the production and consumption of meals.

Create a file called MealQueue.java. In this file you shall implement a thread-safe circular buffer of Meal objects. The constructor shall accept a single integer argument which specifies the maximum size (capacity) of the queue. Additionally, MealQueue shall provide the following methods:

•   putMeal(): accepts a meal as its argument and adds that meal to the end of the queue. If the queue is full, this method should wait until space becomes available.

•   takeMeal(): removes the meal at the front of the queue and returns it. If the queue is empty, this method should wait until a meal becomes available.

•   isEmpty(): returns true if there are no meals in the queue, or false otherwise.

•   isFull(): returns true if the queue is completely full of meals, or false otherwise.

You must implement this class from scratch using a monitor. You are not to use an              ArrayBlockingQueue or any other predefined concurrent Java class as part of your solution.

Hint: Lecture 05 has details on implementing a circular buffer and making it thread-safe.

Modify your Chef thread class so that after printing“Chef prepared...”it places the Meal object into the meal queue. You will need to accept the meal queue as a constructor argument in order to make  this happen.

Two additional source files have been provided for you, Customer.java and Main2.java:

•    Customer.java defines a Customer thread class. A Customer thread repeatedly takes meals    from the queue and eats them. When a Customer thread is interrupted, it prints the total price of all meals that that customer has taken and exits.

•   Main2.java contains the main method. It creates one chef, two customers, and a meal queue with a maximum capacity of 2 meals. Once the Chef thread finishes, the Customer threads  are interrupted using the interrupt() method so that they don’t get stuck waiting on an empty meal queue forever. Using interrupt() is not ideal, because if there are still meals in the meal queue they will not be eaten. Don’t worry about this for now, we will solve it in Part 3.

Example output:

$ java Main2

Chef

Chef

Chef

Chef

Chef

prepared

prepared

prepared

prepared

prepared

Calamari

Bruschetta

Wings

Burger

Curry

Bob is eating Bruschetta ($11)

Alice is eating Calamari ($13)

Alice ate Calamari

Alice is eating Wings ($16)

Bob ate Bruschetta

Bob is eating Burger ($20)

Chef prepared Gnocchi

Chef prepared Schnitzel

Alice ate Wings

Bob ate Burger

Alice is eating Curry ($24)

Bob is eating Gnocchi ($22)

Chef prepared Pumpkin

Chef finished

java.lang.InterruptedException: sleep interrupted

at java.base/java.lang.Thread.sleep(Native Method)

at Customer.run(Customer.java:18)

java.lang.InterruptedException: sleep interrupted

at java.base/java.lang.Thread.sleep(Native Method)

at Customer.run(Customer.java:18)

Bob finished and paid $53

Alice finished and paid $53

Submit Main2.java, Meal.java, Chef.java, MealQueue.java, and Customer.java together as Part_2.zip.

Part 3: Closing time [25 marks]

In this part of the assignment you will modify MealQueue so that the Chef thread can indicate that they have finished preparing meals, allowing Customer threads to finish once the queue is emptied without being interrupted.

Add a new method to MealQueue called close(), and modify the Chef thread so that it calls the    close() method after it has finished preparing meals. Once the meal queue is closed, Customer     threads can continue taking meals. However, when the queue is empty and the queue has been      closed, make it so that null is returned by takeMeal() instead of waiting forever. Modify the         Customer thread so that when takeMeal() returns null, the thread prints the total amount of money paid and exits.

An updated main method has been provided for you in Main3.java. This version of the main method joins the customer threads rather than interrupting them.

Example output:

$ java Main3

Chef

Chef

Chef

Chef

Chef

prepared

prepared

prepared

prepared

prepared

Calamari

Bruschetta

Wings

Burger

Curry

Bob is eating Bruschetta ($11)

Alice is eating Calamari ($13)

Bob ate Bruschetta

Bob is eating Wings ($16)

Alice ate Calamari

Alice is eating Burger ($20)

Chef prepared Gnocchi

Chef prepared Schnitzel

Bob ate Wings

Bob is eating Curry ($24)

Chef prepared Pumpkin

Alice ate Burger

Alice is eating Gnocchi ($22)

Chef finished

Bob ate Curry

Bob is eating Schnitzel ($20)

Alice ate Gnocchi

Alice is eating Pumpkin ($19)

Bob ate Schnitzel

Alice ate Pumpkin

Alice finished and paid $74

Bob finished and paid $71

Submit Main3.java, Meal.java, Chef.java, MealQueue.java, and Customer.java together as Part_3.zip.

Part 4: Vegetarian customers [23 marks]

In this part of the assignment you will enable certain customers to be designated as “vegetarian” customers that do not eat meals containing meat.

Add a new method to MealQueue called takeMealNoMeat(). This method should behave similarly to takeMeal(), except for the fact that it will only return meals that do not contain meat (and will   wait otherwise).

Modify the Customer thread constructor so that it also accepts a boolean indicating whether the customer is vegetarian or note. Vegetarian customers should call takeMealNoMeat() instead of  takeMealMeat() to ensure that they only eat meals that do not contain meat.

An updated main method has been provided for you in Main4.java. This version of the main method specifies that customer“Bob”is vegetarian by passing `true` as the third constructor argument.

Example output:

$ java Main4

Chef

Chef

Chef

Chef

Chef

prepared

prepared

prepared

prepared

prepared

Calamari

Bruschetta

Wings

Burger

Curry

Alice is eating Calamari ($13)

Bob is eating Bruschetta ($11)

Bob ate Bruschetta

Alice ate Calamari

Alice is eating Wings ($16)

Chef prepared Gnocchi

Alice ate Wings

Alice is eating Burger ($20)

Bob is eating Curry ($24)

Chef prepared Schnitzel

Chef prepared Pumpkin

Alice ate Burger

Bob ate Curry

Alice is eating Gnocchi ($22)

Chef finished

Alice ate Gnocchi

Alice is eating Schnitzel ($20)

Bob is eating Pumpkin ($19)

Alice ate Schnitzel

Bob ate Pumpkin

Bob finished and paid $54

Alice finished and paid $91

Submit Main4.java, Meal.java, Chef.java, MealQueue.java, and Customer.java together as Part_4.zip.