need help with this program in java a university wants to de
need help with this program in java
a university wants to demonstrate its political correctness by applying the Supreme court’s “Separate but equal is inherently unequal” doctrine to gender as well as race. As such, it has decided that both genders will use the same bathroom facilities (ie. a unisex lounge). However, in order to preserve some tradition, it decrees that when a woman is in the lounge, only other women may enter, and when a man is in the lounge, only other men may enter. Each unisex lounge will have a sign on the outside indicating what state the lounge is in; Empty, Women Present, or Men Present. Your job is to implement a multi-threaded simulation of this unisex lounge. You may use whatever counters and synchronization mechanisms you need to make sure that your unisex lounge works according to the university decree. Your implementation must generate some status report whenever anyone enters or leaves the lounge which should include the sex of the person entering or leaving.
This status report should include the state of the lounge, the number of men and/or women waiting, and the number and sex of the occupants currently in the lounge. Once a person (thread) enters the lounge, that person’s stay in the lounge should be determined by a random number. The minimum stay should be 2 seconds, and the maximum stay should be 5 seconds. Once a person (thread) has left the lounge, use random numbers to determine when that thread will try to gain access again. I suggest that the wait should range from 4 seconds to 10 seconds. Your program should start 5 male threads and 5 female threads. Each thread should visit the lounge 10 times. Analyze the output to make sure things are working correctly.
Basic Program Structure for USL (UniSex Lounge)
USL Class
This class really does all the work of synchronizing access to the USL. The first question to ask is what information does the USL need to keep. The USL has a state, which reflects if it is empty, or if there are males or females inside. It also must keep track of the number of occupants which are inside, and the number that are waiting to get inside. There can be males or females inside or waiting. The USL must also have a means of blocking a thread which is trying to gain access if the state of the USL does not permit access for this thread at this time. In order to block a thread, the USL can use the wait method which will be used to block threads which must wait until the state of the USL permits access. The USL should keep a counter which indicates how many males or females are blocked waiting to get into the USL. Since the USL has multiple member variables which must be read and written by multiple threads, we must synchronize access to these member variables by making the methods of this classsynchronizedmethods. The constructor for the USL class must initialize all member variables including all the state and counter variables. There should be a maleEnters( ) and a femaleEnters( ) method. These methods check the state to see whether the male or female thread is allowed to enter or must be blocked. Remember, reading and/or writing the member variables must be done inside a critical section which is protected by the synchronized methods. State and/or counter variables must be updated appropriately. If the male or female must wait to enter, the wait method is used to block the thread. When a waiting thread is awakened, it must recheck the state to make sure that it is safe to enter. If the lounge is still in the wrong state, the thread must wait again. Returning from one of these methods indicates that the thread was successful in entering the USL. There should also be a femaleExits( ) and a maleExits( ) method. These methods update the counter and state member variables appropriately. If the thread exiting the lounge is the last one out, the state member variable must change, and any waiting threads must be unblocked so that they can attempt to enter again. The notifyAll method can be used to wake up all the threads which are blocked waiting to get into the USL. Remember that something in your simulation must output the state and counter info frequently, or whenever any thread enters or exits the USL. The easiest way to do this is to have a display method in the USL class and call it from inside the enter and exit methods.
MaleThread and FemaleThread Classes
These classes contain the code that simulates the actions of a male or female thread entering and exiting the USL. These two classes can either inherit from the Thread class or implement the Runnable interface. The constructor for these two classes should accept a USL object so that all the male and female threads are using the same USL object. The run method of these two classes should contain a loop which executes 10 times. Inside the loop, a thread would sleep for a random amount of time between 4 and 10 seconds, and then call the appropriate entry method on the USL. Once that method returns, the thread has gained access to the USL and should wait for between 2 and 5 seconds before calling the appropriate exit method on the USL object.
Main Test Program
This program controls the simulation. The main program must create a USL object to be used by the male and female threads. It must also create 5 MaleThread objects and 5 FemaleThread objects and start all the male and female threads executing.
Solution
These classes contain the code that simulates the actions of a male or female thread entering and exiting the USL. These two classes can either inherit from the Thread class or implement the Runnable interface.
public class CurrentThread
{
public static void main(String arg
s[])
{
Thread thisThread = Thread.currentThread();
try
{
int i;
for( i = 0; i <10 ; i += 2 )
{
System.out.println(i);
Thread.sleep(1000);
}
}
catch (InterruptedException e)
{
System.out.println(“I was interrupted”);
}
}
}
public class SimpleThread extends Thread
{
public SimpleThread(Stri
ng str)
{
super(str);
}
public void run()
{
for (int i = 0; i < 10; i++)
{
System.out.println(i + \" \" + getName());
try
{
sleep((int)(Math.random()
*
1000));
}
catch (InterruptedException e) {}
}
System.out.println(\"DONE! \" + getName());
}
}
public class TwoThreadsTest
{
public static void main (String[] args)
{
new SimpleThread(\"five male \").start();
new SimpleThread(\"five female\").start();
}
}
Once that method returns, the thread has gained access to the USL and should wait for between 2 and 5 seconds before calling the appropriate exit method on the USL object.
Main Test Program
This program controls the simulation. The main program must create a USL object to be used by the male and female threads. It must also create 5 MaleThread objects and 5 FemaleThread objects and start all the male and female threads executing.


