Assignment 0: Java ReentrantReadWriteLock


5/5 - (2 votes)

Programming Assignment 0

In this first (intentionally simple) assignment, you will use a Java
ReentrantReadWriteLock to implement a subset of the
java.util.concurrent.atomic.AtomicLong class, which we call
SimpleAtomicLock. The goal is to understand how to use
ReentrantReadWriteLock to serialize access to a variable that’s shared
by multiple threads. The program creates
two Threads that increment and decrement the AtomicLong 10,000,000
times each. If the SimpleAtomicLong implementation is properly
serialized it’s final value should be 0.

The program shows what happens if concurrent access
to a long isn’t properly serialized. This test program works best on
a multi-core/multi-processor machine, which should actually induce
failure due to race conditions stemming from parallel execution. If
this test “succeeds” then your target platform is not sufficiently
parallel to demonstrate the bug.

In this directory you’ll find the class, which
contains the skeleton Java code that you’ll implement by completing
the “TODO – You fill in here” comments to provide a working solution.
“TODO – You fill in here” portions!!!

In particular, you’ll need to do the following:

. Implement the SimpleAtomicLong class using a Java
ReentrantReadWriteLock, which is covered in this video:

Section 1: Module 2: Part 6: Java ReentrantReadWriteLock

There’s also a good tutorial on ReentrantReadWriteLock at

. Make sure to use a readLock() for AtomicLong.get() and writeLock()
for all the other SimpleAtomicLong methods.

. The program uses various features of Java
Threads and Runnables, which are described in these videos:

Section 1: Module 2: Part 1: Overview of Java Threads (Part 1)
Section 1: Module 2: Part 2: Overview of Java Threads (Part 2)

Likewise, it also uses the Java CountDownLatch and CyclicBarrier
classes, which are described in this video:

Section 1: Module 2: Part 9: Java CountDownLatch

At the moment, all these videos are available in the YouTube playlist

When the 2014 POSA MOOC officially starts these videos will be
available at

We’ll also discuss this assignment specification (and later its
solution) in the POSA MOOC “Virtual Office Hours”, which are described
in item #38 at the POSA MOOC FAQ available from

To compile this code you can either use the provided Eclipse project
or simply type

% javac

on the command-line and then run the resulting class file by typing

% java SimpleAtomicLockTest

The output for a correct solution should look exactly like this:

Starting SimpleAtomicLongTest
test worked
Finishing SimpleAtomicLongTest

PlaceholderAssignment 0: Java ReentrantReadWriteLock
Open chat
Need help?
Can we help?