0

I'm trying to make a thread run my GUI class but every time it runs the run method it completely ignores the while(true) loop i have going and ends up executing nothing, whats strange is that if I add something else to the method such as System.out.println the thread will run.

//GUI is a class I made which implements Runnable        
GUI gui = new GUI();
Thread thread = new Thread(gui);
thread.start();
//Doesn't work, will just ignore the while loop    
@Override
        public void run() {
            while(true){
            //Launches JavaFx application                
               launch();
            }

   }
//If I change it to this or run the program in a debugger it does work 
@Override
        public void run() {
            while(running){
                System.out.println("Blah blah blah");
                  launch();
            }
        }
Minvy
  • 9
  • 4

2 Answers2

0

I tested the following snippet on my local machine : it writes out "Launched" again and again forever...

I guess your problem is somewhere else...

public class Snippet {
    public static void main(String[] args) {
        GUI gui = new GUI();
        Thread thread = new Thread(gui);
        thread.start();
    }
}

//GUI is a class I made which implements Runnable        
class GUI implements Runnable {
    @Override
    public void run() {
        while (true) {
            // Launches JavaFx application
            launch();
        }
    }

    private void launch() {
        System.out.println("Launched!");
    }
}

Maybe try providing some more code ??

Highbrainer
  • 750
  • 4
  • 15
  • Well, having the application launched over and over again could strain and kill the system ;) – Thomas Jun 28 '19 at 12:55
0

First of all I don't know why your launch() is in a loop like @Thomas said. However, I only can say that System.out.println actually outputs the text to standard system output (usually console), and is pretty time consuming when you compare to in memory operations as it is like a 'File stream' that is being operated upon.

This only means that when your 'run' method takes some 'considerable time' to execute, your UI seems to launch. I don't think JVM would not call the launch() method when you've written so, but the 'delay' being introduced by 'System.out.println' is somehow needed for your UI to be launched.

I don't know how to visualize what is going on in your UI code beyond this! Good luck!!

edit: By the way when you say 'debugger' you play 'god' in Debugger by controlling 'thread scheduling', but in runtime 'JVM' is the 'god' who does that. Even that matters.

Siva
  • 598
  • 3
  • 11