Simple program using Java Monitor

355 views Asked by At

I have this simple Java program that utilizes monitors in order to let the customer get into the boarding area. I think I place the wait() and notify() statement in the wrong position that caused the program to deadlock, however, I can not figure it out myself. Below is the code that I wrote.

public class AdultCouple extends Thread
{
    private boolean onRide = false;
    private int ID;

    AdultCouple(int ID)
    {
        this.ID = ID;
    }

    public synchronized void getIn()
    {
        while (!Main.isDoorOpen)
        {
            try
            {
                wait();
            }
            catch (InterruptedException ex)
            {

            }
            System.out.println("Go through");
        }
        System.out.println("Couple " + ID + " get in boarding area.");
        onRide = true;
        Main.peopleInBoardingArea++;
        notify();
    }

    public void run() 
    {
        getIn();
    }
}


public class Area extends Thread
{

    Area()
    {
    }

    public synchronized void openDoor()
    {
        while (Main.peopleInBoardingArea != 0)
        {
            try
            {
                wait();
            }
            catch (InterruptedException ex)
            {
            }
        }
        System.out.println("Area opens");
        Main.isDoorOpen = true;
        notifyAll();
    }

    public synchronized void closeDoor()
    {
    }


    public void run() 
    {
        openDoor();
    }
}


public class ParentKid extends Thread
{

    private boolean onRide = false;
    private int ID;

    ParentKid(int ID)
    {
        this.ID = ID;
    }

    public synchronized void getIn()
    {
        while (!Main.isDoorOpen)
        {
            try
            {
                wait();
            }
            catch (InterruptedException ex)
            {

            }
            System.out.println("Go through");
        }
        System.out.println("Couple " + ID + " get in boarding area.");
        onRide = true;
        Main.peopleInBoardingArea++;
        notify();
    }

    public void run() 
    {
        getIn();
    }

}

public class Main 
{

    public static boolean isDoorOpen = false;
    public static int peopleInBoardingArea = 0;

    public static void main(String args[])
    {
        Thread t3 = new Area();
        Thread t1 = new ParentKid(1);
        Thread t2 = new AdultCouple(2);


        t1.start();
        t2.start();
        t3.start();

        try
        {
            t1.join();
            t2.join();
            t3.join();
        }
        catch (InterruptedException ex)
        {

        }
    }
}
1

There are 1 answers

0
Andrey Cheboksarov On

The problem is that you synchronize on different objects. When you write something like this

public synchronized foo() {...}

you synchornize on this object. In your case you synchronize on current thread which doesn't make any sense. You should synchronize on the same object.

public class Main {

    public static Object lock = new Object();
    ...
}

public class AdultCouple extends Thread
{
    private boolean onRide = false;
    private int ID;

    AdultCouple(int ID)
    {
        this.ID = ID;
    }

    public void getIn()
    {
        synchronized (Main.lock) {
            while (!Main.isDoorOpen) {
                try {
                    Main.lock.wait();
                } catch (InterruptedException ex) {

                }
                System.out.println("Go through");
            }
            System.out.println("Couple " + ID + " get in boarding area.");
            onRide = true;
            Main.peopleInBoardingArea++;
            Main.lock.notifyAll();
        }
    }

    public void run()
    {
        getIn();
    }
}

public class Area extends Thread
{

    Area()
    {
    }

    public void openDoor()
    {
        synchronized (Main.lock) {
            while (Main.peopleInBoardingArea != 0) {
                try {
                    Main.lock.wait();
                } catch (InterruptedException ex) {
                }
            }
            System.out.println("Area opens");
            Main.isDoorOpen = true;
            Main.lock.notifyAll();
        }
    }

    public synchronized void closeDoor()
    {
    }


    public void run()
    {
        openDoor();
    }
}

public class ParentKid extends Thread
{

    private boolean onRide = false;
    private int ID;

    ParentKid(int ID)
    {
        this.ID = ID;
    }

    public void getIn()
    {
        synchronized (Main.lock) {
            while (!Main.isDoorOpen) {
                try {
                    Main.lock.wait();
                } catch (InterruptedException ex) {

                }
                System.out.println("Go through");
            }
            System.out.println("Kid " + ID + " get in boarding area.");
            onRide = true;
            Main.peopleInBoardingArea++;
            Main.lock.notifyAll();
        }
   }


    public void run()
    {
        getIn();
    }

}

Also do not use notify() but notifyAll(). I don't see any reason to use it