Threads: The spaceship airlock  

Worksheet Steps
There are several tasks to complete in this worksheet. Work through each task in turn.

Task 1: Download the code and load into an Eclipse project or equivalent

On Blackboard, you will find the starting code for this project You need to download the files

containing the AstronautSimuIator class and the Door interface.

Note: you can use Eclipse or a different IDE, or none, ifyou want You will need to modi$,' this

code to complete the assignment

Task 2: Look at the existing code and fill in the blanks

Examine the Door interface. Then take a look at the AstronautSimuIator class. This is the one

you need to update. It will not compile because you Will need to create an AirlockDoor class and

an Airlock Class. The following steps need to follow are also listed m the

Astron autSimuIator cla ss :

Step I: Create an AirlockDoor class. Note thatyou will also need to create an

appropriate constructor that takes a String and an Airlock

Step 2: Create an empty Airlock class. This one can remain for the reasons discussed in

the code comments.

Step 3: Create ti•vo thread objects using the Thread class: one for each ofthe two doors. Also

start the threads. You need to do something to the AirlockDoor class, so that your code

compiles. Just do enough to get it to compile.

Step 4: Make a request to open a door (hint: see the interface) and then add an 800 millisecond

delay before trying to open another door. You Will need to add a stubbed out method (empty

and will need implementing in Task 3) to the AirdoorLock class.

Task 3: Implement the Door interface
Step I: Make sure that the AirlockDoor class implements the Door interface. The method you
are forced to add can simply record the open requestin an instance variable. You will need to
decide the type of this instance variable. Add a println statement to the method that says a
request has been made to open the door.
Step 2: You will also need to implement the constructor. Simply store the parameter values in
instance variables. These instance variables will be shared by threads. Refer back to the slides
and lecture examples to see what implication this has on the way you should declare these
instance variables.
Task 4: Implementing the run method
This is where most of the work is required. You will need an infinite loop. You will need to
check whether a door open requesthas been made. Create a critical section of code that:
displays a message saying that the door is about to open;
waits for 5 seconds;
displays a message that the door has closed.
It is important that you can "prove" with println or format statements that the two doors do not
open at the same time.
Have a further I second delay before checking for the another door open request.
Run the simulator to show the program working correctly.
Task 5: Add packages and run from command line
Change all the classes to use suitable package statements. Make sure you use reverse-domain
notation as discussed in lectures In Week 10. Demonstrate thatyou can run your program from
the command line. Make sure the AstronautSimuIator class is in a separate package to the other
classes.
Task 6: Glory
For a further challenge, change the AstronautSimuIator class to be a Thread class and remcwe
the infinite while loop and then create several astronauts (threads) trying to gain access to the
airlock

Powered by