javaswingjdesktop

Why does print unblock execution?


Here is the content of a main function which blocks when the print line is commented, yet it executes as expected when the print line is uncommented.

Why would a single print line change the behavior of the whole while loop? (Between not executing at all, and completing successfully.) This is repeatable, I can comment and uncomment the line any number of times and I get the same result: it only works when the print is uncommented.

Any rational explanation to this strange effect of the print call?

MainGUI.main(args);
DeviceManager device = DeviceManager.getInstance();
MainGUI gui = null;
while(true){
    if(device.getGui() != null){
        gui = laser.getGui();
        if(gui.isLoaded()){
            gui.getMainView().getFrame().setLocation(0, 0);
            break;
        }
    }
    // System.out.print("");
}

Solution

  • The println is likely slowing execution of the tight loop enough to yield the processor to other threads that are necessary for your code to complete. I'll bet a Thread.sleep(5); will do a similar thing.