Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Unisex batroom

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Unisex batroom

    I am having trouble with the Unisex problem in java using thread synchronization. I have four classes the PCB class has all of the methods in it that will be used by the other classes. Then the FemaleThread and MaleThread classes which are similar there are suppose to extend thread, have the PCB member variables ,as well as a construtor and run method that loops 10 times then sleeps random amount of time then enters the bathroom when it is time and then sleeps again, simulating using the bathroom and then finally exits the bathroom. Each thread is suppose to visit the bathroom 10 times and have 5 threads for each Male and Female and only males or females can be in the bathroom at any one time. I am having trouble with the structure of this code. I believe I got the logic behind it but I just have things in the wrong place. Any help that might be out there would be greatly appreciated. I have attached the classes and they are seperated with the file name.
    //PCB.java

    import javax.swing.*;
    import java.awt.*;

    public class PCB
    {
    //function to allow men to enter bathroom
    public synchronized void maleEnters(obj)
    {
    boolean in = false;
    while(!in)
    {
    if(obj.logic == 0) {
    obj.count++;
    obj.logic = 1;
    in = true;

    obj.State = "Bathroom State: Male Present \n";
    System.out.printf("Male entered bathroom \n");
    System.out.printf("%d in bathroom \n", count);
    }
    else if(obj.logic == 1) {
    in = true;
    obj.count++;
    System.out.printf("%d in bathroom \n", count);
    }
    else
    {
    obj.wait++;
    System.out.printf("%d Male in line \n", wait);


    obj.wait--;
    }
    }
    bathroomState();

    }

    //function that allows men to exit the bathroom
    public synchronized void maleExits(obj)
    {
    obj.count--;
    System.out.printf( "Female left bathroom \n");
    if(obj.count == 0)
    {
    obj.logic = 0;
    obj.State = "Bathroom State: Empty \n";
    }
    }

    //function that allows women to enter bathroom if no one is in
    public synchronized void femaleEnters(String State, int logic, int count, int wait, int time_to_exit)
    {
    boolean in = false;
    //WaitForSingleObject(&eventHandler, INFINITE);
    while(!in)
    {
    if(logic == 0)
    {
    obj.count++;
    obj.logic = 1;
    in = true;
    obj.State = "Bathroom State: Male Present \n";
    System.out.printf("Male entered bathroom \n");
    System.out.printf("%d in bathroom \n", count);
    }
    else if(logic == 1)
    {
    in = true;
    obj.count++;
    System.out.printf("%d in bathroom \n", count);
    }
    else
    {
    wait++;
    System.out.printf("%d Male in line \n", wait);
    //LeaveCriticalSection(&cs);
    //WaitForSingleObject(eventHandler, INFINITE);
    obj.wait();
    //EnterCriticalSection(&cs);
    wait--;
    }
    }
    bathroomState();
    }

    //function that allows female to exit
    public synchronized void femaleExits()
    {
    obj.count--;
    System.out.printf( "Male left bathroom \n");
    if(obj.count == 0)
    {
    obj.logic = 0;
    obj.State = "Bathroom State: Empty \n";
    }
    }

    public synchronized void bathroomState()
    {
    System.out.printf(obj.State);
    }
    }

    //FemaleThread
    class FemaleThread extends Thread
    {
    public FemaleThread
    {
    PCB()
    {
    int logic = 0;
    int time_to_exit = 0;
    int count = 0;
    int wait = 0;
    String State = "Bathroom State: Empty \n";
    }
    public void run()
    {
    double r = Math.random();
    for(int i = 0; i < 9; i++)
    {
    //sleepRandom
    sleep(Math.random() * 1000);
    //pcb.enter
    FemaleEnter();
    //sleepRandom
    sleep(Math.random() * 1000);
    //pcb exit
    FemaleExit();
    }
    }
    }
    }
    }
    //MaleThread.java


    public class MaleThread
    {
    class MaleThread implements Runnable
    {
    class PCB extends Thread
    {
    int logic = 0;
    int time_to_exit = 0;
    int count = 0;
    int wait = 0;
    String State = "Bathroom State: Empty \n";

    public void run()
    {
    double r = Math.random();
    for(int i = 0; i < 9; i++)
    {
    //sleepRandom
    sleep(Math.random() * 1000);
    //pcb.enter
    MaleEnter();
    //sleepRandom
    sleep(Math.random() * 1000);
    //pcb exit
    MaleExit();
    }
    }
    }
    }
    }

    //Main.java

    public class Main
    {
    //create PCB
    PCB obj = new PCB();
    //create 5 males give PCB to each
    public class Thread
    {
    public static void main(String[] args)
    {
    // Create five threads and give each one a name.
    Thread m1 = new Thread obj;
    Thread m2 = new Thread obj;
    Thread m3 = new Thread obj;
    Thread m4 = new Thread obj;
    Thread m5 = new Thread obj;
    // Create five threads and give each one a name.
    Thread f1 = new Thread obj;
    Thread f2 = new Thread obj;
    Thread f3 = new Thread obj;
    Thread f4 = new Thread obj;
    Thread f5 = new Thread obj;
    // Start the five male threads
    m1.start();
    m2.start();
    m3.start();
    m4.start();
    m5.start();
    // Start the five female threads
    f1.start();
    f2.start();
    f3.start();
    f4.start();
    f5.start();
    }
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,069
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Unisex batroom

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting