Thread Safety
Get the Assignment Code
Login to your university Linux account and copy the assignment code to a location under your home directory:
cp -r /export/home/public/schwesin/cpsc543/assignments/project1 LOCATION
where LOCATION is a directory of your choosing.
The assignment code is also on D2L.
Overview
The goal for this assignment is to make some existing classes thread safe. The
following files are in the starter code. The ones through TupleExchange.java
require modification. You may need to understand the others. Source files
requiring your work contain STUDENT comments in upper case.
Dataflow.java: Pipeline builder and main. Do this first to see make test explode.MakeUniform.java: Makes a uniform distribution of pseudo-random numbers. It requires added field modifiers to make it thread-safe and some annotations.MakeNormal.java: Makes a normal distribution of pseudo-random numbers. It requires added field modifiers to make it thread-safe and some annotations.MakeExponential.java: Makes an exponential distribution of pseudo-random numbers. It requires added field modifiers to make it thread-safe and some annotations.TenTuple.java: A simple, immutable data container.FiveTuple.java: Slightly more complicated immutable data container.DistributionGenerator.java: First stage active class that invokes one of theMake*generators above.StatisticalAnalysisGenerator.java: Second stage active class that analyzes reduces and reduces the first stage output.StatisticalAnalysisCSVSaver.java: Third stage in pipeline writes entries to a CSV file for diffing.TupleExchange.java: Implicit & explicit locks to synchronize pipeline data handoff. A mutex (lock) with no condition variable and a mutex with a condition variable.IMakeDistribution.java: Interface for the aboveMake*classes.Stats.java: Basic statistics and CSV writing because Java default libraries are not great.Makefile: make fileseed-343458543.ref: reference outputjcip-annotations.jar: Annotations for the textbook denote design intent.
Note: Stats.java does not require changes, but it has an example line of code
useful in improving FiveTuple.java's thread safety.
Here are some basic guidelines for making this assignment’s classes thread safe:
Make private any data field that can be private.
Inert data containers like
TenTuplecan have public fields i.f.f. those fields are immutable, for example primitive data types, or references to immutable objects such as String or most (all?)java.lang.Numberobjects. Mutable objects such as arrays should be private so as not to escape their objects.Fields should be final when possible. Making a reference to a mutable object final does not make it immutable, but it does guarantee flushing to main memory when the constructor returns. A mutable field should be either volatile, or have every access guarded by an implicit or explicit lock, or have a final reference to a thread-safe object from package
java.util.concurrent.atomicWe will cover atomic later.Use the textbook
@Immutable,@ThreadSafe,@NotThreadSafe, and@GuardedByannotations as required inSTUDENTcomment to communicate design intent.
Perform all test execution on arya to avoid any platform-dependent output
differences.
Use make clean test to compile and test your code changes. Testing must
occur on arya.
The handout single-main-thread test passes, but the multi-threaded solution won't pass until you complete your work. A successful test does not guarantee that all requirements are met because a race condition that allows tests to pass sometimes is possible. Please re-read all STUDENT requirements before turning it in. When make test does not report an error, testing has succeeded.
$ cat multi.csv
Distribution,Param1,Param2,Count,Mean,Median,Mode,Pstdev,Min,Max
The multi-threaded output is missing its data. That is only a header line.
Turning in the Assignment
For this assignment, you must turn in a zip file of a directory named
project1 containing your edited files. Submit the zip file to the appropriate
folder on D2L.
Dataflow Diagram for this assignment