SE 552: Homework

Deadline: 5.30pm, Tuesday 13 April, 2004

Homework

Download the source archive again, and make sure you can compile and run the TestBuffer application:

  cd src
  javac ajeffrey/teaching/test/TestBuffer.java
  java ajeffrey.teaching.test.TestBuffer

When you run the application you will probably see something like:

  A got Fred
  B got Wilma

and debugging output will appear in the file TestBufferDebug.txt.

You should now recompile the application with step debugging on, by uncommenting the line:

  // Debug.out.addFactory (StepDebugStream.factory);

1. Use the step debugger to find an execution of the program which prints:

  A got Fred
  B got null

2. Edit the UnsafeBuffer.java file so that the resulting structure is thread-safe (in particular so that checkInvariants() can never fail). You should do this by adding a new lock object to the class, and by making appropriate use of synchronized.

Save the resulting file as SafeBuffer.java, and compile and test it. Your code should contain debugging messages each time a lock is acquired and released (following the style in Guard.java).

3. Using your favourite zip archive tool, create a zip archive containing the TestBufferDebug.txt file you generated for part 1, and the SafeBuffer.java file you edited for part 2, and submit the archive using Courses OnLine.