• Post Reply Bookmark Topic Watch Topic
  • New Topic

why it prints the statement in wrong order or fashion?  RSS feed

 
Ranch Hand
Posts: 692
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sometimes the output is expected ie
  • thread started
    i m about to stop the thread !

  • and some times its output is truly unexpected
    like
  • i m about to stop the thread !
    thread started


  • can any one explain me , why this happens ?

     
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    System.out and System.err are two independent streams that both get funneled to your terminal window. Just because you send to "out" first, that doesn't mean that "out" will necessarily make it to the terminal first. Use either "out" or "err" for both println() calls.

    Also:

    1) Make your "done" variable volatile, or synchronize all access to it on the same lock. In this case, it won't matter, since the same thread that's writing it is also reading it, but in general, you'll have a different thread telling this thread to stop by setting some shared variable, and if you don't use volatile, synchronization, or java.util.concurrent.AtomicXxx, then there's no guarantee when, or even if, one thread will see another thread's write.

    2) Don't compare X to true or false, just look at X:

     
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    naved momin wrote:sometimes the output is expected ie
  • thread started
    i m about to stop the thread !

  • and some times its output is truly unexpected
    like
  • i m about to stop the thread !
    thread started

  • can any one explain me , why this happens ?

    Because the console is receiving inputs from two sources (System.out and System.err), and I wouldn't be at all surprised if it has Threads of its own that are checking for them.

    Hi naved. Long time no see - well, couple of days, anyway.

    Winston
     
    naved momin
    Ranch Hand
    Posts: 692
    Eclipse IDE Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Winston Gutkowski wrote:
    naved momin wrote:sometimes the output is expected ie
  • thread started
    i m about to stop the thread !

  • and some times its output is truly unexpected
    like
  • i m about to stop the thread !
    thread started

  • can any one explain me , why this happens ?

    Because the console is receiving inputs from two sources (System.out and System.err), and I wouldn't be at all surprised if it has Threads of its own that are checking for them.

    Hi naved. Long time no see - well, couple of days, anyway.

    Winston

    ya thanks for asking , by the way my examz are on sem 5th winter , thats why i m little busy , with my studies .
     
    naved momin
    Ranch Hand
    Posts: 692
    Eclipse IDE Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jeff Verdegan wrote:System.out and System.err are two independent streams that both get funneled to your terminal window. Just because you send to "out" first, that doesn't mean that "out" will necessarily make it to the terminal first. Use either "out" or "err" for both println() calls.

    can you explain me , this behavior of out stream and err stream
  • if i write System.out first & then System.err why my out will not print ahead of errr ?

  •  
    lowercase baba
    Bartender
    Posts: 12565
    49
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    because the JVM and/or OS are multi-threaded. That means they switch back-and-forth between different mini-processes very quickly. You are not guaranteed execution order. So what happens (in this case) is that you write to Standard.out, which is paused for a moment while Standard.err runs. you write to that, it prints out the output, then switches back to Standard.out, which then writes to the console.

    It is possible that sometimes when you run it, you get the order reversed. You have no control over this. Your options are basically to write everything to the same stream, or re-direct one stream to a file or something else.
     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    naved momin wrote:ya thanks for asking , by the way my examz are on sem 5th winter , thats why i m little busy , with my studies .


    Please, UseRealWords. Seriously. That kind of writing is horribly painful to read.
     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    naved momin wrote:
    Jeff Verdegan wrote:System.out and System.err are two independent streams that both get funneled to your terminal window. Just because you send to "out" first, that doesn't mean that "out" will necessarily make it to the terminal first. Use either "out" or "err" for both println() calls.

    can you explain me , this behavior of out stream and err stream
  • if i write System.out first & then System.err why my out will not print ahead of errr ?



  • Winston already explained it: They can each have separate threads transferring their data from your println() calls to the OS's output device.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!