3

Every now and again I want to use System.out.prinln to debug things instead of using a debugger, or I want a simple program to write to standard output so that I can log something without taking the time to get proper logging set up. I've noticed that sometimes my text winds up getting printed out of order. E.g.:

System.out.println("A");
System.out.println("B");    
System.out.println("C");

might result in

A
C
B

being printed.

I'm pretty sure I'm not crazy, so I have two questions:

  1. Why does this happen?
  2. What's an easy way I can keep this from happening?

EDIT: More information:

I'm running unit tests that build Lucene queries with JUnit. To print them out I've written this class:

public class LogHelper { //TODO-DAVID remove
    public static final boolean ENABLED = true;
    public static final boolean DISABLED = false;

    private boolean enabled;

    public LogHelper(boolean enabled){
        this.enabled = enabled;
    }

    public synchronized void debug(String someString){
        if(enabled){
            System.out.println(someString);
        }
    }
}

I tried making 'debug()' synchronized, just in case multiple threads were calling it, but strange printing still occasionally happens out of order.

David
  • 14,569
  • 34
  • 78
  • 107
  • 2
    I don't mean to sound like a broken record, but there's almost no way we're going to be able to shed light on this without an SSCCE. – Dennis Meng Oct 19 '13 at 00:39
  • 3
    If some of those are actually System.err.println, you'll get some interleaving on the console because System.out is buffered but System.err is always flushed. – rob Oct 19 '13 at 00:40
  • Unless you show us an actual concrete (complete) example, we can't explain what it going on. There are a number of *possible* explanations. – Stephen C Oct 19 '13 at 00:45
  • 1
    +1 for you're not crazy :) – Konstantin Yovkov Oct 19 '13 at 00:49
  • @DennisMeng and @_Stephen C, as I said in my post: this happens infrequently and in multiple simple programs. Because of it only happens some of the time, even in the same program, I couldn't possibly give you an SSCCE. I suppose I could write a for-loop that print things, run it until (at some indeterminate point in the future) it finally prints things out of order, and then upload it. But that's a waste of both our time. – David Oct 19 '13 at 00:55
  • @DennisMeng and @_Stephen C, I've added a little bit more information. It's not quite an SSCCE, more of an S_CCE, but it's what I have time for right now. – David Oct 19 '13 at 01:02
  • @Kocko, thanks. I'll show this to the staff here. It's hard to type in a a straight jacket. It'd be nice if I could take it off. – David Oct 19 '13 at 01:04
  • Finaly, @StephenC, do you merely mean that there are an infinite number of theoretically possible explanations, or do specific ones come to mind? If the latter, can you suggest them? – David Oct 19 '13 at 01:16
  • @David - No I don't mean there are an infinite number of theoretically possible explanations. Don't be ridiculous! Given what you've added, there is only one likely explanation; see the accepted Answer. – Stephen C Oct 19 '13 at 04:51

2 Answers2

10

That will never happen unless the printing is happening in different threads. Then execution order may be indeterminate. If, on the other hand, you're writing to System.out and System.err and seeing jumbled output, that's because those are two different streams that happen to write to the exact same output by default, and one or the other may come out first, especially due to buffering or other considerations.

Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
  • thanks for your answer. I've updated my question above to indicate that I've synchronized my call to system.out.println() but am still seeing odd behavior. Any ideas about this? – David Oct 19 '13 at 01:07
  • The `synchronized` in your example will in no way prevent multiple threads from printing things out of order if that is, indeed, what's happening. It just means only one thread can print at a time. My answer stands. – Ryan Stewart Oct 19 '13 at 01:21
  • ahh. Yah that makes sense now. And it appears that Junit does run tests concurrently: http://stackoverflow.com/questions/7267790/does-junit-execute-test-cases-sequentially Answer accepted. – David Oct 19 '13 at 01:29
0

I'm sure will never happen with your example on a single thread..

Anyway, make sure you don't have function calls inside System.out.println( that might create new thread..specially functions that call OS System.. they usually mess up...

Jose Almeida
  • 54
  • 1
  • 6