簡體   English   中英

為什么我的程序有時會卡住?

[英]why does my program get stuck sometimes?

因此,我使用監視器在 Java 中編寫了一個有界緩沖區問題,但我無法弄清楚我的程序有什么問題。 有時它會在第三個循環結束之前一直在無限循環中運行。 大多數時候它運行完美。 該程序很簡單,關於一個生產者和多個消費者。 我將不勝感激任何幫助。 這是我的 github 的鏈接,您可以在其中找到整個代碼。 完整代碼

有界緩沖區

public class BoundedBuffer {
    public BoundedBuffer ()
    {
        int numWorms = 10;
        int numBirds = 5;

        Dish dish = new Dish (numWorms);
        Parent parent = new Parent(dish);
        parent.start();
        Child child[] = new Child[numBirds];
        for (int i = 0; i < numBirds; i++)
        {
            child[i] = new Child (dish);
            child[i].start();
        }       
        for (int i = 0; i < numBirds; i++)
        {
            try {child[i].join();}
            catch (Exception ie) {System.out.println (ie.getMessage());}
        }

        System.out.println("bids done eating :D");
    }
}

盤子

public class Dish 
{
    int worms;
    int copy;
    public Dish (int worms)
    {
        this.worms = worms;
        copy = worms;
    }
    public synchronized void eat ()
    {
        if (worms <= 0)
        {

            waitForFull();
        }
        worms --;
        System.out.println("Bird " + Thread.currentThread().getName() + " has 
eaten."
                        + " The number of worms left is " + worms);

    }
    public synchronized void fill()
    {
        if (worms > 0)
        {
            waitForEmpty();
        }
        worms = copy;
        System.out.println ("Parent filled the dish");
        notifyAll();
    }
    public synchronized void waitForEmpty ()
    {
        while (worms > 0)
        {
            notifyAll();
            try {wait();}
            catch (Exception ie) {System.out.println (ie.getMessage());}
            }
}
    public synchronized void waitForFull ()
    {
        while (worms <= 0)
        {
            notifyAll();
            try {wait();}
            catch (Exception ie) {System.out.println (ie.getMessage());}
        }
    }
}

我無法重現stuck情況。 但是我在您的代碼中發現了一些其他問題:

  • Parent set done = 1在三個循環后立即完成,此時Dish中仍然有Worms
  • waitForEmptyfill不是自動的,這可能會導致一些不一致。
  • waitForFulleat不是自動的,這可能會導致一些不一致。

兩個解決這些問題,我認為你需要:

  • 合並waitForEmptyfill作為單個方法,以及waitForFulleat
  • 使用shutdownisTerminate作為信號。 Parentshutdown Dish ,而最后一個Childterminate Dish

這是代碼:

主要的

public class Main {
    public static void main(String[] args) {
        new BoundedBuffer ();
    }
}

有界緩沖區

public class BoundedBuffer {
    public BoundedBuffer () {
        int numWorms = 10;
        int numBirds = 5;

        Dish dish = new Dish (numWorms);
        Parent parent = new Parent(dish);
        parent.start();
        Child child[] = new Child[numBirds];
        for (int i = 0; i < numBirds; i++) {
            child[i] = new Child (dish);
            child[i].start();
        }       
        for (int i = 0; i < numBirds; i++) {
            try {child[i].join();}
            catch (Exception ie) {System.out.println (ie.getMessage());}
        }

        System.out.println("bids done eating :D");
    }
}

盤子

public class Dish {
    int worms;
    int copy;
    boolean shutDown;
    boolean isTerminated;

    public Dish (int worms) {
        this.worms = worms;
        copy = worms;
    }

    public synchronized void waitForEmptyToFill() {
        while (worms > 0) {
            try {
                notifyAll();
                wait();
            } catch (Exception ie) {
                System.out.println (ie.getMessage());
            }
        }
        worms = copy;
        System.out.println ("Parent filled the dish");
        notifyAll();
    }

    public synchronized void waitForFullToEat () {
        while (worms <= 0 && !isTerminated()) {
            try {
                notifyAll();
                wait();
            } catch (Exception ie) {
                System.out.println (ie.getMessage());
            }
        }
        if (worms > 0) {
            worms--;
            System.out.println("Bird " + Thread.currentThread().getName() + " has eaten."
                    + " The number of worms left is " + worms);
            if (worms == 0 && isShutDown()) {
                setTerminated(true);
                notifyAll();
            }
        }
    }


    public synchronized boolean isShutDown() {
        return shutDown;
    }

    public synchronized void setShutDown(boolean shutDown) {
        this.shutDown = shutDown;
    }

    public synchronized boolean isTerminated() {
        return isTerminated;
    }

    public synchronized void setTerminated(boolean terminated) {
        isTerminated = terminated;
    }
}

家長

public class Parent extends Thread {

    private Dish dish;

    public Parent (Dish dish) {
        this.dish = dish;
    }

    public void run() {
        for (int i = 0; i < 3; i++) {
            dish.waitForEmptyToFill();
        }
        dish.setShutDown(true);
    }
}

孩子

public class Child extends Thread {
    private Dish dish;
    public Child (Dish dish)
    {
        this.dish = dish;
    }
    public void run () {
        while (!dish.isTerminated()) {
            dish.waitForFullToEat();
            try {
                sleep(100);
            } catch (Exception ie) {
                System.out.println (ie.getMessage());
            }
        }
    }
}

輸出:

Bird Thread-4 has eaten. The number of worms left is 9
Bird Thread-3 has eaten. The number of worms left is 8
Bird Thread-2 has eaten. The number of worms left is 7
Bird Thread-5 has eaten. The number of worms left is 6
Bird Thread-1 has eaten. The number of worms left is 5
Bird Thread-2 has eaten. The number of worms left is 4
Bird Thread-3 has eaten. The number of worms left is 3
Bird Thread-4 has eaten. The number of worms left is 2
Bird Thread-1 has eaten. The number of worms left is 1
Bird Thread-5 has eaten. The number of worms left is 0
Parent filled the dish
Bird Thread-4 has eaten. The number of worms left is 9
Bird Thread-5 has eaten. The number of worms left is 8
Bird Thread-3 has eaten. The number of worms left is 7
Bird Thread-1 has eaten. The number of worms left is 6
Bird Thread-2 has eaten. The number of worms left is 5
Bird Thread-4 has eaten. The number of worms left is 4
Bird Thread-5 has eaten. The number of worms left is 3
Bird Thread-1 has eaten. The number of worms left is 2
Bird Thread-2 has eaten. The number of worms left is 1
Bird Thread-3 has eaten. The number of worms left is 0
Parent filled the dish
Bird Thread-1 has eaten. The number of worms left is 9
Bird Thread-5 has eaten. The number of worms left is 8
Bird Thread-3 has eaten. The number of worms left is 7
Bird Thread-2 has eaten. The number of worms left is 6
Bird Thread-4 has eaten. The number of worms left is 5
Bird Thread-2 has eaten. The number of worms left is 4
Bird Thread-3 has eaten. The number of worms left is 3
Bird Thread-1 has eaten. The number of worms left is 2
Bird Thread-5 has eaten. The number of worms left is 1
Bird Thread-4 has eaten. The number of worms left is 0
Parent filled the dish
Bird Thread-2 has eaten. The number of worms left is 9
Bird Thread-3 has eaten. The number of worms left is 8
Bird Thread-1 has eaten. The number of worms left is 7
Bird Thread-5 has eaten. The number of worms left is 6
Bird Thread-4 has eaten. The number of worms left is 5
Bird Thread-2 has eaten. The number of worms left is 4
Bird Thread-3 has eaten. The number of worms left is 3
Bird Thread-1 has eaten. The number of worms left is 2
Bird Thread-5 has eaten. The number of worms left is 1
Bird Thread-4 has eaten. The number of worms left is 0
bids done eating :D

暫無
暫無

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

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