11 Replies Latest reply: Oct 23, 2011 11:31 AM by 835847 RSS

    Synchronized use of System.out.println()

    835847
      So I am again at the point of part of my program running ahead of itself out of the call stack and ruining the sequence or order of printing.

      I read that System.out.println() is synchronized so how may it be used to wait() until notified to continue and not ruin the output?
        • 1. Re: Synchronized use of System.out.println()
          835847
          So without the use of synchronization or threading, can I establish a PrintStream order that is satisfied every time? In other words, lexicographic or alphabetical like order for printing one thing then the next, then the next, then the next guaranteed. I am not using threads, main launches the program then dies and everything else proceeds in functional order. Problem is the variance in the return from the stack is causing PrintStream to vary the order of printing.

          Maybe someone understand this problem.
          • 2. Re: Synchronized use of System.out.println()
            EJP
            part of my program running ahead of itself out of the call stack and ruining the sequence or order of printing.
            I don't understand this sentence.
            Problem is the variance in the return from the stack is causing PrintStream to vary the order of printing.
            Or this one either.
            Maybe someone understand this problem.
            Try describing it more clearly.
            • 3. Re: Synchronized use of System.out.println()
              835847
              In laymen's terms, the printing is out of order from what it should be. If a, b, and c are objects that should print in the order a, b, c then I am seeing printing in the order a, c, b and b, a, c so I was wondering if I could somehow synchronize System.out.
              • 4. Re: Synchronized use of System.out.println()
                EJP
                I believe it's already synchronized internally. Maybe the order of events isn't what you think it is? You could try synchronizing on it yourself and calling both println() and flush() in the synchronized block.
                • 5. Re: Synchronized use of System.out.println()
                  835847
                  Could I do that without turning my classes into threads?

                  I am thinking all that is needed is:
                  synchronized(System.out){
                       System.out.println("Whatever");
                       System.out.flush();
                  }
                  I was reading over at StackOverflow that flush() won't do the job but I am all ears if you know how.
                  • 6. Re: Synchronized use of System.out.println()
                    EJP
                    Could I do that without turning my classes into threads?
                    Who said anything about turning all your classes into threads?
                    I am thinking all that is needed is:
                    I just suggested exactly that.
                    • 7. Re: Synchronized use of System.out.println()
                      835847
                      You got me a little scared here EJ becuase I am thinking my program will be throwing IllegalStaeMonitorExceptions but I will try it. Interesting this is it is .out and .err that are printing in incorrect orders so I may as well try and synchronize both at the same time.
                      • 8. Re: Synchronized use of System.out.println()
                        EJP
                        I am thinking my program will be throwing IllegalStaeMonitorExceptions
                        Why on earth are you thinking that?The only methods in the whole JDK that throws that are Object.wait() and Object.notify(), and you aren't calling either of them. You don't have any reason to think that, any more than you had any reason to think about turning all your classes into Threads. You seem to be just creating difficulties where none exist frankly.

                        If your problem is actually that System.out and System.err "are printing in 'incorrect' orders", try printing everything to one of them. For example in a catch block, print your own message to System.err and then call exc.printStackTrace(). Or else print to System.out and call exc.printStackTrace(System.out). Or use java.util.logging or some other logging package.
                        • 9. Re: Synchronized use of System.out.println()
                          835847
                          795833 wrote:
                          I am thinking my program will be throwing IllegalStaeMonitorExceptions
                          Why on earth are you thinking that?The only methods in the whole JDK that throws that are Object.wait() and Object.notify(), and you aren't calling either of them. You don't have any reason to think that, any more than you had any reason to think about turning all your classes into Threads. You seem to be just creating difficulties where none exist frankly.

                          If your problem is actually that System.out and System.err "are printing in 'incorrect' orders", try printing everything to one of them. For example in a catch block, print your own message to System.err and then call exc.printStackTrace(). Or else print to System.out and call exc.printStackTrace(System.out). Or use java.util.logging or some other logging package.
                          YES SIR EJP! SIR! I thank you for the assistance because using only System.out and synchronizing worked beautifully. Problem solved. Nos if only I could figure out dependencies to make it work with out and err.
                          • 10. Re: Synchronized use of System.out.println()
                            796440
                            Always Learning wrote:
                            YES SIR EJP! SIR! I thank you for the assistance because using only System.out and synchronizing worked beautifully.
                            You said you're not using multiple threads. If this is true, then there's no reason to synchronize anything. The output will appear on System.out in exactly the order you send it there.
                            Nos if only I could figure out dependencies to make it work with out and err.
                            You have to stop and think about it for a minute. You know that when you call println() on either one of those two streams, the output may be buffered an not necessarily go immediately to the console. From that you can reason that if you call out.println() first, and then err.println(), that you could end up with err's buffer getting flushed first, and the output appearing on the console in a different order than that in which your code executed the calls.

                            You are of course not surprised by this, given that you know that out and err are completely independent and just happen to end up at the same destination in this particular case.

                            So, as a first guess, you might reasonably think that, since buffering is obviously the culprit here, calling flush() on each stream after each print() or println() call should eliminate the problem. In a multithreaded environment, this wouldn't be sufficient, of course, but it's a logical approach to try here.

                            Another tidbit to make note of is that the System class has setOut() and setErr() calls. Since you're looking at out and err in the same console, you presumably don't care about the distinction between them (which makes me wonder why you're using them both in the first place, instead of just using one). If you're just going to mush them together into the same console anyway, then you can use setOut() or setErr() to make them the same stream, and things will be ordered as you expect.
                            • 11. Re: Synchronized use of System.out.println()
                              835847
                              jverd wrote:
                              Always Learning wrote:
                              YES SIR EJP! SIR! I thank you for the assistance because using only System.out and synchronizing worked beautifully.
                              You said you're not using multiple threads. If this is true, then there's no reason to synchronize anything. The output will appear on System.out in exactly the order you send it there.
                              Nos if only I could figure out dependencies to make it work with out and err.
                              You have to stop and think about it for a minute. You know that when you call println() on either one of those two streams, the output may be buffered an not necessarily go immediately to the console. From that you can reason that if you call out.println() first, and then err.println(), that you could end up with err's buffer getting flushed first, and the output appearing on the console in a different order than that in which your code executed the calls.

                              You are of course not surprised by this, given that you know that out and err are completely independent and just happen to end up at the same destination in this particular case.

                              So, as a first guess, you might reasonably think that, since buffering is obviously the culprit here, calling flush() on each stream after each print() or println() call should eliminate the problem. In a multithreaded environment, this wouldn't be sufficient, of course, but it's a logical approach to try here.

                              Another tidbit to make note of is that the System class has setOut() and setErr() calls. Since you're looking at out and err in the same console, you presumably don't care about the distinction between them (which makes me wonder why you're using them both in the first place, instead of just using one). If you're just going to mush them together into the same console anyway, then you can use setOut() or setErr() to make them the same stream, and things will be ordered as you expect.
                              Very interesting Jverd and I think there may yet be life in what I would like to do. I did not know or was not immediately aware of these things. I will give it a try.

                              To answer your question, I am using them both because, like logging, they are distinct in the Eclipse console (black for out and red for err). With your patch I just tried that distinction has faded but the output is sequenced the same so I do appreciate you noting this. Learn something new in Java each time I am doing a project. I considered using logging and putting errors in a window but I am not sure if I should do that; just not enough experience with it.

                              Edited by: Always Learning on Oct 23, 2011 9:28 AM