This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Stumbled across something strange today

 
Danjel Nyberg
Greenhorn
Posts: 15
Eclipse IDE Java MS IE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was just tweaking a bit and stumbled across something strange, which i can't explain

given this simple class:


this gives different output every time i run it...

expected output is:

0
1
2
3
4
Exception in thread "main" java.lang.Exception
at SCJP.Test18.main(Test18.java:9)

but if I run it several times it could give me this output:

0
1Exception in thread "main" java.lang.Exception
at SCJP.Test18.main(Test18.java:9)

2
3
4

My only explenation to this is that the update of output to the Eclipse console is not keeping up.

Any ideas or input?


/Danjel
 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It gives me all time first output.Although my thought about your second output, i don't care, it isn't topic of research.
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's because the System.out.println() invocations go to the standard output stream (System.out) and the stack trace goes to the standard error stream (System.err). Two different buffered streams, which may or may not get flushed in the same order you invoke them. A simple way to make sure the output is always ordered the way you expect, in a single-threaded application, is to assign the same output stream to both System.out and System.err, using the System.setOut() and System.setErr() methods.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic