Java 是通过循环和代码 运行 提前传递?

Java is by passing loop and code running ahead?

我从没见过 Java 这样做,但它似乎绕过了我的循环。

我正在使用带有 2 个 classes 的 slick2D。

图形

public class 图形扩展 BasicGame {

public static int POS1 = 100;
public static int POS2 = 100;
public static String text = "f";
public static boolean test = false;
public static boolean test3 = false;
public static boolean w = false;
public static boolean m = false;
public static boolean left = false;
public static boolean right = false;
Boolean test2 = false;

public graphic(String gamename){
    super(gamename);
}
public void start() {
    try{

    AppGameContainer appgc;
    appgc = new AppGameContainer(new graphic("ProjectES"));
    appgc.setDisplayMode(1300, 800, false); //width, height
    appgc.start();



    }catch(SlickException es){
        System.out.println("MAJOR ERROR, GRAPICH.START");
    }
}

@Override
public void render(GameContainer gc, Graphics g) throws SlickException {
    g.setColor(Color.white);
    g.drawString("Y " + Main.year + " M" + Main.month + " W" + Main.week, 550, 10);


    if(Objectsellinghandler.test == true){
        //g.fillRect(100, 100, 500, 150); //window
        Image popup1 = new Image("res/Window.png");
        popup1.draw(370,250);
        g.setColor(Color.orange);
        g.drawString(Main.text0, 380,260);
        g.drawString(Main.text1, 380,280);
        g.drawString(Main.text2, 380,300);
        g.drawString(Main.text3, 380,320);
        g.drawString(Main.text4, 380,340);
    }

    if(Objectsellinghandler.test == true){
        //g.fillRect(100, 100, 500, 150); //window
        Image popup1 = new Image("res/Window.png");
        popup1.draw(370,250);
        g.setColor(Color.orange);
        g.drawString(Main.text0, 380,260);
        g.drawString(Main.text1, 380,280);
        g.drawString(Main.text2, 380,300);
        g.drawString(Main.text3, 380,320);
        g.drawString(Main.text4, 380,340);
    }


    //Image img = new Image("res/image.png");
    //img.draw(POS1,POS2);

}
@Override
public void init(GameContainer arg0) throws SlickException {
    // TODO Auto-generated method stub


}
@Override
public void update(GameContainer gc, int arg1) throws SlickException {

    Input input = gc.getInput();
    Main.MainInput = input.toString();

    if(test2 == false){
        Main.MainInput = "w";
        text = "gg";
        System.out.println("gg");
        test2 = true;

        Main.year++;
        Thread time = new Thread(new Time());
        time.start();


    }
    if(input.isKeyDown(Input.KEY_W) && test3 == false){
        test3 = true;
        test = true;
        w = true;
        m = false;
        left = false;
        right = false;
        System.out.println("test1111");
        Thread t1 = new Thread(new Objectsellinghandler());
        t1.start();
    }
    if(input.isKeyDown(Input.KEY_M)){
        m = true;
        w = false;
        left = false;
        right = false;
        System.out.println("RLLY");
    }
    if(input.isKeyDown(Input.KEY_LEFT)){
        left = true;
        m = false;
        w = false;
        right = false;
    }
    if(input.isKeyDown(Input.KEY_RIGHT)){
        right = true;
        left = false;
        m = false;
        w = false;
    }

    Main.MainInput = "";
    //if(){
        //test2 = true;
//      System.out.println("WHY ARE YOU RUNNING");
        //Thread t1 = new Thread(new Objectsellinghandler());
        //t1.run();
//  }

    }

}

对象销售处理程序。

public class Objectsellinghandler implements Runnable{

public static boolean test = false;


public void beginning(){


}


@Override
public void run() {
    System.out.println("gg");
    test = true;
    if(Main.rep < 1){
            Main.text0 = "You are starting out with $" + Main.money + " from a bank loan, to";
            Main.text1 = "start you off, were buying 100 units of a red lamp";
            Main.text2 = "from china, for 00.";
            Main.text4 = "Press M to continue.";

            int I = 0;
            while(graphic.m = false){

                System.out.println(I);
            }
            System.out.println(graphic.m);
            graphic.m = false;
            Main.MainInput = "";
            /*Main.text0 = "";
            Main.text1 = "";
            Main.text2 = "";
            Main.text4 = "";
            */
        Main.money = Main.money - RedlampI.priceperhundred;
        RedlampI.amount = RedlampI.amount + 100;
        Main.text0 = "The items have been purchased, it will be here in one";
        Main.text1 = "month";

        double month = Main.month;
        double monthcomparing = month + 1;
        System.out.println("month " + Main.month + "monthcomparing " + monthcomparing + "Main month" + Main.month);
        while(monthcomparing != month){
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            month = Main.month;

        }
        System.out.println("month " + Main.month + "monthcomparing " + monthcomparing + "Main month" + Main.month);
        Main.text0 = "The shipment is here, shipping costed 0";
        Main.money = Main.money - 200;
        Main.text1 = "You have " + Main.money + " money left, A good price is  but you";
        Main.text2 = "can choose any price between  and  for this item.";

        Main.text4 = "Press M to continue.";


        I = 0;
        while(graphic.m = false){

            System.out.println(Main.MainInput);

        }
        graphic.m = false;
        Main.MainInput = "";
        test = false;
        Main.text0 = "";
        Main.text1 = "";
        Main.text2 = "";

        Main.text4 = "";
        RedlampI objectselling1 = new RedlampI();
        objectselling1.onsale();
        Thread.currentThread().interrupt();
        return;
        }

}
}

等待布尔值 "m" 的循环被自动绕过!虽然是假的。

而且这段代码好像是运行在线程创建之前!

double month = Main.month;
        double monthcomparing = month + 1;
 monthcomparing + "Main month" + Main.month);

这个多线程真的非常奇怪,已经超过 24 小时了,我不明白为什么。

while(graphic.m = false) 除了是一个自旋锁,因此是一件坏事,它是对 graphic.m 的赋值,并且总是立即为假。

使用 while(graphic.m == false) 会使您的代码 "correct" 但仍然很糟糕。使用 while(!graphic.m) 没有那么糟糕,但使用更好的等待策略是正确的做法。

更新: 现在想想,这应该会产生一个 "unreachable code" 错误。 false 的编译时常量仅在 if 中允许,而不是 while.