我的多线程应用程序不会循环

时间:2014-11-26 01:04:33

标签: java multithreading

我正在尝试创建一个程序,该程序将一直运行直到用户停止它。它集中在圣诞老人的工作室,我有圣诞老人,九个驯鹿和十一个精灵。十一个精灵必须制造100个玩具(有5%失败的机会,圣诞老人帮助),九个驯鹿必须从假期返回。当两件事都发生时,圣诞老人送出玩具。在此之后,九只驯鹿去度假,精灵们必须再创造100多只玩具。这是我的代码:

main.java

public class Main {

    private static Santa santa;
    public Semaphore elfSemaphore = new Semaphore(1);
    public Semaphore reindeerSemaphore = new Semaphore(1);
    public volatile int toyList = 100;
    public volatile int reindeersOnHoliday = 9;

    public static void main(String[] args){

        Main main = new Main();

        santa = new Santa(main);


        Reindeer dasher = new Reindeer("Dasher", main);
        Reindeer dancer = new Reindeer("Dancer", main);
        Reindeer prancer = new Reindeer("Prancer", main);
        Reindeer vixen = new Reindeer("Vixen", main);
        Reindeer comet = new Reindeer("Comet", main);
        Reindeer cupid = new Reindeer("Cupid", main);
        Reindeer dunder = new Reindeer("Dunder", main);
        Reindeer blitzen = new Reindeer("Blitzen", main);
        Reindeer rudolph = new Reindeer("Rudolph", main);

        Elf elfOne = new Elf("Jim", main);
        Elf elfTwo = new Elf("John", main);
        Elf elfThree = new Elf("Eamonn", main);
        Elf elfFour = new Elf("Eoin", main);
        Elf elfFive = new Elf("Ronan", main);
        Elf elfSix = new Elf("Seamus", main);
        Elf elfSeven = new Elf("Rebecca", main);
        Elf elfEight = new Elf("Orla", main);
        Elf elfNine = new Elf("Tina", main);
        Elf elfTen = new Elf("Filly", main);
        Elf elfEleven = new Elf("Jess", main);

        santa.start();

        dasher.start();
        dancer.start();
        prancer.start();
        vixen.start();
        comet.start();
        cupid.start();
        dunder.start();
        blitzen.start();
        rudolph.start();

        elfOne.start();
        elfTwo.start();
        elfThree.start();
        elfFour.start();
        elfFive.start();
        elfSix.start();
        elfSeven.start();
        elfEight.start();
        elfNine.start();
        elfTen.start();
        elfEleven.start();

    }

    public void wakeSanta(String reason) throws InterruptedException {
        santa.setReasonForWake(reason);

    }

}

elf.java

public class Elf extends Thread{

    Random rng = new Random();
    String name;
    Main main;

    public Elf(String name, Main main){
        this.name = name;
        this.main = main;
    }

    public void run(){
        while(true){
                try {
                    makeToy();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
        }
    }

    private void makeToy() throws InterruptedException{
        main.elfSemaphore.acquire();
        if (main.getToyList() != 0){
            if(rng.nextInt(99) <= 4){
                System.out.println(name + " failed in making a toy! They went to Santa for help.");
                main.wakeSanta("problem");
            }else{
                main.setToyList(main.getToyList() - 1);
                System.out.println(name + " created a toy! Toys remaining: " + main.getToyList());
            }
        } else {
            main.wakeSanta("toys");
        }
        main.elfSemaphore.release();
    }

}

reindeer.java

public class Reindeer extends Thread {

    String name;
    boolean onHoliday;
    Main main;

    public Reindeer(String name, Main main){
        this.name = name;
        this.onHoliday = true;
        this.main = main;
    }

    public void run(){
        while(true){
            if(onHoliday){
                try {
                    returnFromHoliday();
                    if(name.equals("Rudolph") && main.getReindeersOnHoliday() == 0){
                        main.wakeSanta("reindeers");
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else {
                if (main.getReindeersOnHoliday() == 9){
                    onHoliday = true;
                }
            }
        }
    }

    private void returnFromHoliday() throws InterruptedException {
        onHoliday = false;
        System.out.println(name + " says: I'm back from holiday!");
        main.reindeerSemaphore.acquire();
        main.setReindeersOnHoliday(main.getReindeersOnHoliday() - 1);
        main.reindeerSemaphore.release();

    }

}

santa.java

public class Santa extends Thread {

    Main main;
    private boolean isSleeping = true;
    private String reasonForWake = "";

    public Santa(Main main){
        this.main = main;
    }

    public void run(){
        while(true){
            if(!isSleeping){
                if(reasonForWake.equals("reindeer")){
                    if(main.getToyList() == 0){
                        System.out.println("*****************************\nTime for Santa to deliver presents!\n*****************************");
                        deliverPresents();
                    } else{
                        isSleeping = true;
                    }
                } else if (reasonForWake.equals("problem")){
                    try {
                        main.elfSemaphore.acquire();
                        main.setToyList(main.getToyList() - 1);
                        System.out.println("Santa has helped an elf fix a problem. Toys left: " + main.getToyList());
                        isSleeping = true;
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } finally {
                        main.elfSemaphore.release();
                    }
                } else { //toys
                    if(main.getReindeersOnHoliday() == 0){
                        System.out.println("*****************************\nTime for Santa to deliver presents!\n*****************************");
                        deliverPresents();
                    }
                    else {
                        isSleeping = true;
                    }
                }
            }
        }
    }

    public void setReasonForWake(String reasonForWake) {
        isSleeping = false;
        this.reasonForWake = reasonForWake;
    }

    private void deliverPresents(){
        isSleeping = true;
        main.setReindeersOnHoliday(9);
        main.setToyList(100);
    }
}

它可以使用一年,但它似乎卡在run()的{​​{1}}中,并且从未实际传递玩具,这意味着它永远不会循环。我觉得main,elf和reindeer中的代码非常坚固,但我不确定圣诞老人。任何人都可以提供有关如何使其可靠循环的见解吗?

0 个答案:

没有答案