Lab 10: Concurrency

À quelle heure le train arrive-t-il ?

--One of the four phrases I remember from my month listening to "Learn French in your Car" tapes.

The goal of this lab is to practice using concurrency.

1. Introduction and set up

You will be working with a train simulation. There are two trains running on elliptical tracks that meet at four points. In the code as you get it, they will (almost certainly) crash at the crossings. You need to modify the code so that they do not crash.

Make a new directory for this lab and copy the files that I am giving you.

cp -r ~tvandrun/Public/cs245/lab10/trains .

Do not use Eclipse for this lab. (The reason is that for several of the classes involved in this lab I am giving you only the compiled (.class) files. Eclipse doesn't play nicely when given only .class files.)

Instead, use a text editor (gedit or xemacs or whatever you prefer) to open the files Train.java, Track.java, and TrackPosition.java.

Track is an interface showing you how to interact with the track. You can call methods to make a train advance on the track or to determine what position a train is or where it will go next if it advances.

TrackPosition is an interface showing you how to interact with a track position. The one thing you really care about is whether or not this position is a crossing.

Train is the Runnable for threads that control how trains go. You need to fix the run method so that trains don't "plow ahead forever".

2. Your task

Try running the program as it is and confirm that the trains crash. The class with the main method is trains.Trains.

java trains.Trains

Then fix it. All the coding you need to do is in Train.java. The only new thing you need to know about Java is how locks, monitors, and synchronized stuff works.

Trains take up six track positions at a time---but since they are moving, it's often a good idea to give them a little space.

Don't forget to recompile Train.java. If you're used to working in Eclipse, this is an easy thing to forget.


Thomas VanDrunen
Last modified: Thu Apr 3 10:16:40 CDT 2014