Sie sind auf Seite 1von 2

Unisex Bathroom Problem

CA463 – Martin Crane


Due: 17th December 2004

Daniel Hunt
51451987
daniel.hunt4@mail.dcu.ie

“Suppose there is only one bathroom in your school. It can be used by both men
and women, but not by both at the same time. Develop a Java program that
solves this problem. Allow at most 5 men or women in the bathroom at the same
time, but ensure that your solution is fair to both sexes.”

1. Project Files:
 ToiletProblem.java
 ToiletThread.java
 Person.java
 Globals.java
 Semaphore.java
 TOILET.MF (jar manifest file)
 Compile.bat, go.bat, and package.bat
 images/cubicle-empty.png, images/cubicle-man.png, and images/cubicle-
woman.png

2. How this project works


By using a “Façade Pattern”, I created an application that successfully
encompasses the scope of this project.
When run, the program first sets up all User-Interface related objects, and then
prepares the system for runtime. Once ready, the user can select the “Start” menu
item from the “Main” menu at the top left of the screen.

This item creates a new instance of “ToiletThread” and executes it’s run() method.
ToiletThread is the actual functional part behind this program, as ToiletProblem is
simply a User-Interface. It begins by creating an instance of Globals to be used by
the program, as well as a random sequence of men/women in order to simulate a
busy toilet in a school.
When ToiletThread’s threaded section is started, it tells all “Person” threads to
start as well, before entering ‘reporter-mode’ where it informs the User-Interface
of the current status of the toilet.
A “Person” can be either a Man or a Woman. The key difference between these
two, is that a Woman takes twice as long in a cubicle as a Man, and therefore
means that the toilet will remain locked to women only for a longer period of time
than to men. Note that this is not giving preference to men or women. It is simply
an attempt to mirror real-life situations.

This program creates an initial set of people to use the toilet, and does not create
them “on the fly” during runtime. This was to simplify the design and
implementation of the problem presented to us. The creation of men and women is
entirely random, and it cannot be known in advance what the sequence of sexes in
the queue is.

It is worth noting that if, at any stage, both a man and a woman are discovered to
be in the toilet at the same time, the program will display an error on screen for
you to acknowledge.

3. Assumptions made
Due to the nature of this problem, I have assumed that the Thread.wait() and
Thread.notify() methods that are built into java, automatically allow the longest
waiting thread to re-activate. This is what I have based my algorithm on, and I
find that it works perfectly.

4. Satisfaction of Fairness
When a person enters the queue and attempts to enter the bathroom, the
Semaphore checks the person’s counter (or ticket number) to see if it is less than
the current highest ticket number in the toilet. If the ticket number is greater than
the current highest number in the toilet, then the person is told to wait, otherwise
the person may proceed to the second check which consists of comparing their
own sex to that of the current people in the toilet, if any exist.

This ensures that no one skips the queue – creating a completely fair system.

I have included a zip file containing the 5 java source files, 3 batch files for
compiling/building/executing the project, the jar manifest file, all required images, as
well as the javadocs relating to my project and this document.
As well as this you can find an executable jar file to run a compiled version of this
program.

Daniel Hunt

Das könnte Ihnen auch gefallen