0

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("");
}
Lucie
  • 125
  • 1
  • 1
  • 7
  • I guess your `System.out.println` line is outside the `while(true)` code block, otherwise there's something else in the code that cause this. – Luiggi Mendoza Jan 09 '14 at 19:42
  • @LuiggiMendoza: no, it's inside. it's outside the two if() blocks, but still inside the while() – Marc B Jan 09 '14 at 19:42
  • 1
    Use a debugger to figure out where the loop is blocking, and what it's blocking on. All will be revealed. – Brian Kelly Jan 09 '14 at 19:43
  • I'm guessing the print call flushes caches or in some other way causes the internal state of the system to change, forcing it to actually do the device.getGui() test again, eventually allowing it to get to the inner code and break. – Marc B Jan 09 '14 at 19:43
  • @MarcB I know by this code that is inside, but probably is a typo. – Luiggi Mendoza Jan 09 '14 at 19:43
  • @BrianKelly when I use a debugger it's work without the print... – Lucie Jan 09 '14 at 19:56
  • @MarcB may be but how can I do that without the print ? – Lucie Jan 09 '14 at 19:57

1 Answers1

2

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.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373