简体   繁体   English

Java - while(true) 循环不检查,如果条件为真,如果缺少 else 语句

[英]Java - while(true) loop not checking, if condition is true, if the else statement is missing

public class Main {

    public static boolean isToggled = false;
    public static boolean isClicking = false;

    public static void main(String[] args){
        while(true){
            if(Main.isToggled && Main.isClicking){
                System.out.println("Running");
            }            
        }
    }

I have two public static booleans with the value of false in my Main class.我的主 class 中有两个公共 static 布尔值,其值为 false。

A while(true) loop will constantly check, if both booleans are set to true, by another class.如果两个布尔值都设置为 true,while(true) 循环将不断检查另一个 class。

However, if both conditions are being set to true, nothing happens.但是,如果两个条件都设置为 true,则不会发生任何事情。

The while loop only works, if an else statement is added to it like this:只有在像这样添加 else 语句时,while 循环才有效:

public class Main {

    public static boolean isToggled = false;
    public static boolean isClicking = false;

    public static void main(String[] args){
        while(true){
            if(Main.isToggled && Main.isClicking){
                System.out.println("Running");
            }
            else{
                System.out.println("Idle");
            }            
        }
    }

So it is definitely the while loop that is the problem.所以问题肯定是while循环。

The class which sets the conditions to true works perfectly fine.将条件设置为 true 的 class 工作得非常好。 I've tested that by removing the while loop, and just printing the value of the booleans after their value has changed.我已经通过删除 while 循环进行了测试,并在布尔值更改后仅打印它们的值。

I don't understand, why the first code isn't doing what its supposed to, when the other one with the else statement added to it does.我不明白,为什么第一个代码没有做它应该做的事情,而另一个添加了 else 语句的代码却做了。

Copy code to test:复制代码进行测试:

public class Main {

    public static boolean isToggled = false;
    public static boolean isClicking = false;

    public static void main(String[] args) {

        Thread changeBoolsT = new Thread(new Runnable() {
            @Override
            public void run() {
                changeBools();
            }
        });
        changeBoolsT.start();


        while(true){
            if(isToggled && isClicking){
                System.out.println("OK");
                break;
            }
            else{ // Remove this else statement
                System.out.println("Waiting");
            }
        }
    }

    public static void changeBools(){
        try{
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        isToggled = true;
        isClicking = true;
    }
}

The JVM will optimize your code, because the value is never changed in your main thread. JVM 将优化您的代码,因为您的主线程中的值永远不会改变。 There is no synchronization happening between threads.线程之间没有同步发生。 You need to use synchronized blocks when reading and writing the variables or mark them as volatile .您需要在读取和写入变量时使用synchronized块或将它们标记为volatile volatile will force them to be read anew from memory every time they are accessed. volatile将强制每次访问它们时从 memory 重新读取它们。

I dont think its your loop, its that you are trying to access static variables within your main method.我不认为它是你的循环,它是你试图在你的主要方法中访问 static 变量。 A solution would be to instantiate a main object in your main class.一个解决方案是在你的主 class 中实例化一个主 object。

Main mainObj = new Main();   

then your while look like那么你的 while 看起来像

while(true){
     if(mainObj.isToggled && mainObj.isClicking){
            System.out.println("Running");
        }
        else{
            System.out.println("Idle");
        } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM