簡體   English   中英

使用Java Monitor的簡單程序

[英]Simple program using Java Monitor

我有這個簡單的Java程序,它利用監視器讓客戶進入登機區。 我想我將wait()和notify()語句放在導致程序死鎖的錯誤位置,但是,我自己無法弄明白。 下面是我寫的代碼。

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)
        {

        }
    }
}

問題是您在不同的對象上同步。 當你寫這樣的東西

public synchronized foo() {...}

你同步這個對象。 在您的情況下,您在當前線程上同步,這沒有任何意義。 您應該在同一個對象上同步。

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();
    }

}

也不要使用notify()但使用notifyAll()。 我認為沒有理由使用它

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM