Hello, I need this done with Java and ran in Eclipse ifpossible.
A university wants to demonstrate its political correctness byapplying the Supreme Court’s “Separate but equal is inherentlyunequal†doctrine to gender as well as race. As such, theuniversity has decided that both genders will use the same bathroomfacilities. However, in order to preserve some tradition, itdecrees that when a woman is in the bathroom, only other women mayenter, and when a man is in the bathroom, only other men may enter.Each politically correct bathroom will have a sign on the outsideindicating what state the bathroom is in; Empty, Women Present, orMen Present.
Your job is to implement a multi-threaded simulation of thispolitically correct bathroom. You may use whatever counters andsynchronization mechanisms you need to make sure that yourpolitically correct bathroom works according to the universitydecree.
Your implementation must generate some status report wheneveranyone enters or leaves the bathroom, which should include the sexof the person entering or leaving. This status report shouldinclude the state of the bathroom, the number of men and/or womenwaiting, and the number and sex of the occupants currently in thebathroom.
Once a person (thread) enters the bathroom, that person’s stayin the bathroom should be determined by a random number. Theminimum stay should be 2 seconds, and the maximum stay should be 5seconds.
Once a person (thread) has left the bathroom, use random numbersto determine when that thread will try to gain access again. Isuggest that the wait should range from 4 seconds to 10seconds.
Your program should start 5 male threads and 5 female threads.Each thread should visit the bathroom 10 times. Analyze the outputto make sure things are working correctly. Capture a sample of yourprogram’s status reports. Also, print out your source code.Demonstrate your program to the professor, and get a sign off.
Basic Program Structure for PCB (Politically CorrectBathroom)
PCB Class
This class really does all the work of synchronizing access tothe PCB. The first question to ask is what information the PCBneeds to keep. The PCB has a state, which reflects whether it isempty or whether there are males or females inside. It also mustkeep track of the number of occupants which are inside, and thenumber who are waiting to get inside. There can be males or femalesinside or waiting.
The PCB must also have a means of blocking a thread that istrying to gain access if the state of the PCB does not permitaccess for this thread at this time. In order to block a thread,the PCB can use the wait method, which will be used toblock threads that must wait until the state of the PCB permitsaccess. The PCB should keep a counter which indicates how manymales or females are blocked and waiting to get into the PCB.
Since the PCB has multiple member variables which must be readand written by multiple threads, we must synchronize access tothese member variables by making the methods of this classsynchronized methods.
The constructor for the PCB class must initialize all membervariables, including all the state and counter variables.
There should be a maleEnters( ) method and a femaleEnters( )method. These methods check the state to see whether the male orfemale thread is allowed to enter or must be blocked. Remember,reading and/or writing the member variables must be done inside acritical section which is protected by the synchronized methods.State and/or counter variables must be updated appropriately. Ifthe male or female must wait to enter, the wait method isused to block the thread. Returning from one of these methodsindicates that the thread was successful in entering the PCB.
There should also be a femaleExits( ) method and a maleExits( )method. These methods update the counter and state member variablesappropriately. If the thread exiting the bathroom is the last oneout, the state member variable must change, and any waiting threadsmust be unblocked so that they can attempt to enter again. ThenotifyAll method can be used to wake up all the threadsthat are blocked and waiting to get into the PCB.
Remember that something in your simulation must output the stateand counter info frequently, or whenever any thread enters or exitsthe PCB.
MaleThread and FemaleThread Classes
These classes contain the code that simulates the actions of amale or female thread entering and exiting the PCB. These twoclasses can either inherit from the Thread class or implement theRunnable interface. The constructor for these two classes shouldaccept a PCB object so that all the male and female threads areusing the same PCB object. The run method of these two classesshould contain a loop which is executed 10 times. Inside the loop,a thread would sleep for a random amount of time between 4 and 10seconds, and then call the appropriate entry method on the PCB.Once that method returns, the thread has gained access to the PCBand should wait for between 2 and 5 seconds before calling theappropriate exit method on the PCB object.
Main Test Program
This program controls the simulation. The main program mustcreate a PCB object to be used by the male and female threads. Itmust also create 5 MaleThread objects and 5 FemaleThread objectsand start all the male and female threads executing.
It is up to you how to display the output from your simulation.You can use a console window and report the status every time athread enters, starts waiting, stops waiting, or exits the PCBobject. Or you can use a simple GUI and use the main thread toperiodically (every 100 msec) update a display that shows in someway the threads that are in the PCB or that are waiting to get in,and whether they are male or female threads.