Description
6pm
These instructions require you use Java. All code should be well-commented, in a professional style, with appropriate variables names, indenting, etc. Your code must be clear and readable. Marks will be very generously
deducted for bad style or lack of clarity.
In this assignment you must use blocking synchronization in all solutions. Do not spin, and do not use tryLock or other APIs that allow for a locking or acquire operation to timeout or fail.
All shared variable access must be properly protected by synchronization—there must be no race conditions, but also no unnecessary use of synchronization. Unless otherwise specified, your programs should aim to be efficient, and exhibit high parallelism, maximizing the ability of threads to execute concurrently. Please stick closely to the described input and output formats.
1. Suppose you have an n vertex 2D polygon with the property that for each boundary point p, a ray extended 15 from the center (origin) through p does not intersect with a similar ray extended from the center to any other boundary point p0 (a star property).
Here you will repeatedly modify such a polygon using multiple threads. Define a class/file star.java that accepts two command-line parameters m and c, such that m ≤ n, and c ≥0.
In the program, first create the polygon, representing it by a doubly-linked list of n vertices (x,y pairs and next/prev references). Hard-code a polygon based on the following (circular) sequence of vertices:
(-1.0,5.0)
(1.0,2.0)
(5.0,0.0)
(1.0,-2.0)
(-4.0,-4.0)
(-3.0,-1.0)
Once the polygon is constructed, launch m threads. Each thread does the following c times:
(a) Choose a random vertex, v.
(b) Move the vertex to a random point within the triangle formed by v.prev,v,v.next (c) Sleep for 30ms.
Access to each vertex is to be protected by its own lock (or synchronized block). Do not use any other synchronization.
Once each thread has done c modifications, draw the resulting polygon, scaled down to fit a 1920×1080 bitmapped (png) image and saving it as a file output.png.
Your solution should guarantee that the resulting polygon retains the original star property.
2. A cat consists of a body, tail, 2 forelegs which have 5 toes and 2 hindlegs which have 4 toes, a head, 2 eyes, and 6 whiskers . They are created by an assembly process. Infinite bins of each individual part, bodies, tails, legs, toes, heads, eyes, and whiskers, is available. The cat is then assembled by robots doing the following specialized tasks.
1
(a) Toes. A leg is acquired, and (randomly) either 4 toes or 5 toes are acquired and attached, producingeither a single foreleg or hindleg. This takes 10–20ms.
(b) Legs. A body is acquired, with or without a tail, and 4 legs are attached to the body to give it 2
forelegs and 2 hindlegs. This takes 30–50ms.
(c) Tail. A body, with or without legs, is acquired along with 1 tail to form a more complete body. Thistakes 10-20ms.
(d) Eyes. A head, with or without whiskers, is acquired along with 2 eyes to form a more completehead. This takes 10-30ms.
(e) Whiskers. A head, with or without eyes, is acquired along with 6 whiskers to form a more completehead. This takes 20-60ms.
(f) Cat. A head with both eyes and whiskers is attached to a body that has all 4 legs and a tail. Thistakes 10-20ms.
Create a simulation, catmaker.java that models this assembly process. Represent each robot by a separate thread, mimicking the assembly work times through sleep. Run the program until 250 complete cats have been produced by the final cat assembler robot; other than this robot, other robots should not keep count of the number of parts produced.
As output, emit proportion of total simulation time that each robot has spent idle (not “working”).
(a) Produce a solution using monitors, based on synchronized and wait / notify / notifyAll. 15
What to hand in
Where possible hand in only source code files containing code you write. Do not submit compiled binaries or .class files. For any written answer questions, submit either an ASCII text document or a .pdf file with all fonts embedded. Do not submit .doc or .docx files. Images (plots or scans) are acceptable in all common graphic file formats. 15
This assignment is worth 10% of your final grade.
45
2
Reviews
There are no reviews yet.