1

At the very beginning of my program, I print out these two lines:

Dyvil Compiler 1.0.0 for Dyvil 1.0.0

Loading Configuration File from 'config.txt'

The first is printed in System.err, while the second is in System.out. Both println calls happen in the main method in the above order. However, it appears that the messages are sometimes swapped:

Loading Configuration File from 'config.txt'

Dyvil Compiler 1.0.0 for Dyvil 1.0.0

This happens when running the program in Eclipse Debug mode or from a Gradle build script (I haven't tested it with the java command). Note that absolutely no multithreading is involved, so don't tell me about the definition of insanity.

What is going on here?

Community
  • 1
  • 1
Clashsoft
  • 11,553
  • 5
  • 40
  • 79

3 Answers3

2

The two streams are exactly that, two different streams. As a result, if you eventually merge them together into a console output of some sort then it is perfectly feasable for the lines to appear out of order.

Both System.out and System.err are PrintStreams and are therefore almost certainly buffered so it just depends on when they get flushed.

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
0

It's a bug in Eclipse - "ordering of output sent to stdout and stderr is not deterministic". See https://bugs.eclipse.org/bugs/show_bug.cgi?id=9720

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
-1

You could always just add the sleep(#ofseconds) command just before the second println but after the first println to make sure it gets printed second. That will print the first line, wait however many seconds you choose, then print the second line. Its kind of a work around but it might fix the problem.

brandon Whe
  • 206
  • 1
  • 14
  • 2
    More reliable to use `flush()`. – OldCurmudgeon Jul 20 '15 at 15:00
  • `sleep()` seldom fixes any problem: It usually just makes the problem less likely to occur. Maybe a lot less likely. In my world, that means, a lot less likely to occur in _testing_. So, if the problem ever _does_ occur, it probably will happen at a customer site. :-O – Solomon Slow Jul 20 '15 at 16:24