关于后端:COMP124-计算机体系

24次阅读

共计 7333 个字符,预计需要花费 19 分钟才能阅读完成。

University of Liverpool
Department of Computer Science
COMP124 – Computer Systems
Coursework 2 – Multi-Threaded Java Programming
Deadline: Thursday 13th May at 17:00
Weighting: 15%
Follow the instructions below to submit your work. Penalties for late work will be applied in accordance
with the Code of Practice on Assessment. The standard department marking descriptors apply to this
assessment.
IMPORTANT – How to Submit
The filename of a Java program must have the same name as the public class containing its main method.
To avoid compilation problems later on (and when we mark it) please stick to this naming scheme. If your
username is sgnabog and your student ID is 201355426, then…
• Your Java source file should be named sgnabog_201355426.java
• Your main class declaration should be public class sgnabog_201355426 {…}
Regardless of good programming practice, all the classes should be defined in one file. Submit just this
single .java file containing all the classes that make up your solution (via the assessment page on Canvas).
Please don’t submit any other documents. We can only mark your work if we can easily locate and open
the file. You will lose marks if we have to manually change your filename to compile and run your solution.
Overview
The purpose of this assessment is to test your ability to write a compact, multi-threaded Java solution
based on a simple producer-consumer problem. In brief…
• Potter A produces a pot at the rate of one every 5 minutes
• Potter B produces a pot at the rate of one every 6 minutes
• After producing a pot, the potter puts it on a shelf
• The shelf can only store a maximum of 5 pots at any one time
• The packer takes pots off the shelf at a rate of one every 4 minutes
• The packer cannot do any work if the shelf is empty
• The potters cannot make another pot until they have placed their current one on the shelf
Write a multi-threaded Java program that simulates the above scenario. You will need threads for each
potter, and another for the packer. Your program should continue until each potter has made 10 pots and
the packer has packed all 20 pots. As the program runs, it should output a commentary on what is
happening. An example is included at the end of this document. Your output doesn’t have to be identical,
and in fact you will get different output each time you run your program. You can change the wording if
you like, but the commentary must still cover all the important actions of each thread (and the shelf).
Useful Hints
You need to simulate time passing, so the potters and packer can work at the speeds shown above. The
sleep() method takes a parameter representing the number of milliseconds to sleep for. This effectively
pauses the thread for that length of time. Represent each minute as 100ms, just so the program runs a bit
faster.
try {
sleep(500);
} catch(InterruptedException e) {}
The code above will simulate taking 5 minutes to make a pot, so it should be included somewhere within
the run() method of one of the potters. The other potter should sleep for 600ms, and the packer should
sleep for 400ms.
Base your solution on the example code for the producer-consumer problem. Keep things as simple as
possible. At first, you might think that an array is a good way to represent the shelf, but this would actually
complicate things a lot. There is a really simple way to represent whether or not the shelf has room for
another pot, and you don’t really need any parameter to the insert() method because the pots don’t
need to exist as objects in the code.
A good, optimal solution will be fairly small, around 100 lines depending on how many comments you
include. If your code is significantly more than this, it’s an indication that you’re going down the wrong
path. Remember to include all the classes for the solution in a single source file.
Code Comments & Structure
Use the Java comment notation (comments start with // or are enclosed with //) to place useful
explanations within the code. However, do not write lengthy comments that get in the way of readability.
Include your student ID as a comment at the top of your code.
Structure and indent your code properly, so it’s easy to read and understand. Use meaningful variable,
class and method names. As you will be including multiple classes in the same source file, position them so
that the code is readable and makes sense from top to bottom.
Note that you do not have to write and submit a report for this assessment. Submit only a single .java
source file. Therefore, make use of comments to explain briefly what your code does.
Marking Breakdown
Your work will be marked according to the following criteria.
Style: 20% – Overall layout, presentation and structure of the code
Programming: 20% – Correctness and succinctness of the code
Concurrency: 40% – Efficient use of Java classes and threads to model the problem
Output: 20% – Accuracy and usefulness of output as the program runs
Development Environment
We will compile and run your solution on the command line (using the javac and java commands). You
can use whatever environment you like to develop your solution. As there are numerous ways to develop
Java code, we cannot provide technical support for anything other than basic text editor and commandline
usage.
Example Output
Ginny has started
Harry has started
Albus has started
Ginny is making a pot
Harry is making a pot
Albus is ready to pack
Shelf is empty (Waiting…)
Shelf inserted a pot and now has 1 pots
Harry has put a pot on the shelf
Harry is making a pot
Shelf removed a pot and now has 0 pots
Shelf inserted a pot and now has 1 pots
Ginny has put a pot on the shelf
Ginny is making a pot
Albus has packed pot 1
Albus is ready to pack
Shelf removed a pot and now has 0 pots
Shelf inserted a pot and now has 1 pots
Harry has put a pot on the shelf
Harry is making a pot
Shelf inserted a pot and now has 2 pots
Ginny has put a pot on the shelf
Ginny is making a pot
Albus has packed pot 2
Albus is ready to pack
Shelf removed a pot and now has 1 pots
Shelf inserted a pot and now has 2 pots
Harry has put a pot on the shelf
Harry is making a pot
Albus has packed pot 3
Albus is ready to pack
Shelf removed a pot and now has 1 pots
Shelf inserted a pot and now has 2 pots
Ginny has put a pot on the shelf
Ginny is making a pot
Shelf inserted a pot and now has 3 pots
Harry has put a pot on the shelf
Harry is making a pot
Albus has packed pot 4
Albus is ready to pack
Shelf removed a pot and now has 2 pots
Shelf inserted a pot and now has 3 pots
Ginny has put a pot on the shelf
Ginny is making a pot
Shelf inserted a pot and now has 4 pots
Harry has put a pot on the shelf
Harry is making a pot
Albus has packed pot 5
Albus is ready to pack
Shelf removed a pot and now has 3 pots
Albus has packed pot 6
Albus is ready to pack
Shelf removed a pot and now has 2 pots
Shelf inserted a pot and now has 3 pots
Ginny has put a pot on the shelf
Ginny is making a pot
Shelf inserted a pot and now has 4 pots
Harry has put a pot on the shelf
Harry is making a pot
Albus has packed pot 7
Albus is ready to pack
Shelf removed a pot and now has 3 pots
Shelf inserted a pot and now has 4 pots
Harry has put a pot on the shelf
Harry is making a pot
Shelf inserted a pot and now has 5 pots
Ginny has put a pot on the shelf
Ginny is making a pot
Albus has packed pot 8
Albus is ready to pack
Shelf removed a pot and now has 4 pots
Shelf inserted a pot and now has 5 pots
Harry has put a pot on the shelf
Harry is making a pot
Albus has packed pot 9
Albus is ready to pack
Shelf removed a pot and now has 4 pots
Shelf inserted a pot and now has 5 pots
Ginny has put a pot on the shelf
Ginny is making a pot
Shelf is full (Waiting…)
Albus has packed pot 10
Albus is ready to pack
Shelf removed a pot and now has 4 pots
Shelf inserted a pot and now has 5 pots
Harry has put a pot on the shelf
Harry is making a pot
Shelf is full (Waiting…)
Albus has packed pot 11
Albus is ready to pack
Shelf removed a pot and now has 4 pots
Shelf inserted a pot and now has 5 pots
Ginny has put a pot on the shelf
Ginny is making a pot
Shelf is full (Waiting…)
Albus has packed pot 12
Albus is ready to pack
Shelf removed a pot and now has 4 pots
Shelf inserted a pot and now has 5 pots
Harry has put a pot on the shelf
Shelf is full (Waiting…)
Albus has packed pot 13
Albus is ready to pack
Shelf removed a pot and now has 4 pots
Shelf inserted a pot and now has 5 pots
Ginny has put a pot on the shelf
Ginny is making a pot
Albus has packed pot 14
Albus is ready to pack
Shelf removed a pot and now has 4 pots
Shelf inserted a pot and now has 5 pots
Ginny has put a pot on the shelf
Albus has packed pot 15
Albus is ready to pack
Shelf removed a pot and now has 4 pots
Albus has packed pot 16
Albus is ready to pack
Shelf removed a pot and now has 3 pots
Albus has packed pot 17
Albus is ready to pack
Shelf removed a pot and now has 2 pots
Albus has packed pot 18
Albus is ready to pack
Shelf removed a pot and now has 1 pots
Albus has packed pot 19
Albus is ready to pack
Shelf removed a pot and now has 0 pots
Albus has packed pot 20

正文完
 0