0
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.

A while(true) loop will constantly check, if both booleans are set to true, by another class.

However, if both conditions are being set to true, nothing happens.

The while loop only works, if an else statement is added to it like this:

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.

The class which sets the conditions to true works perfectly fine. I've tested that by removing the while loop, and just printing the value of the booleans after their value has changed.

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.

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;
    }
}
6
  • I am not sure what you are doing but the name of your boolean varibales make me think you should use a listener instead Commented Sep 24, 2022 at 16:14
  • Does this answer your question? [stackoverflow.com/questions/58041588/…) Commented Sep 24, 2022 at 16:22
  • @xthe_white_lionx i am using a listener to check for key presses, to then change the boolean values. in my main class i just want to check if the booleans have been set to true, to then execute another task Commented Sep 24, 2022 at 16:29
  • @JohannesKuhn im not sure if that helps me, its not the threading that is the problem, the code below is just an example. I just cant figure out why the very first code isnt working but the second is, where the only thing changed is that there has been an else statement with another sysout added to it. Commented Sep 24, 2022 at 17:00
  • 1
    Yeah, sysout synchronizes. Without synchonization it will read the old value. Commented Sep 24, 2022 at 17:04

2 Answers 2

4

The JVM will optimize your code, because the value is never changed in your main thread. There is no synchronization happening between threads. You need to use synchronized blocks when reading and writing the variables or mark them as volatile. volatile will force them to be read anew from memory every time they are accessed.

Sign up to request clarification or add additional context in comments.

Comments

-1

I dont think its your loop, its that you are trying to access static variables within your main method. A solution would be to instantiate a main object in your main class.

Main mainObj = new Main();   

then your while look like

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

3 Comments

This way should allow you to set your variables as private in order to adhere to good coding practices.
but i need the variables to be public static, so that they can be accessed in another class to change their values. the code below was just an example
Could you make a Main object member in the other classes. Instantiate the Main object in the main class, then use a setter method in your other classes to set the Main object in your main class to those classes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.