Something to do with the different PrintStreams (System.err and System.out)...?
If this behavior is reproducible for you (it's not for me), then you should be able to determine if it's the PrintStreams by switching err and out. [ June 25, 2007: Message edited by: marc weber ]
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
Bharat Makwana
Ranch Hand
Joined: May 21, 2007
Posts: 107
posted
0
Thanks you Mr.weber
dhwani mathur
Ranch Hand
Joined: May 08, 2007
Posts: 621
posted
0
well!! please if anyone can tell me if i am right here,in the below shown code,the class test calls the the constructor of Class Foo so it prints Foo const
dhwani: Yes, static variables are initialized when the class is loaded, which happens when the constructor is called but before the body of the constructor executes. This is why we would expect "truth Called" to be output before "Foo Const."
Bharat: I've tried this code many times on different machines (Mac and Windows), and I have not observed the odd behavior. Are you able to reproduce this? Are you able to change the behavior by switching PrintStreams?
dhwani mathur
Ranch Hand
Joined: May 08, 2007
Posts: 621
posted
0
Thanks marc for clearing my doubt,but i tried running the same program on my PC using eclipse IDE at first it printed
truth called Foo const but second time can say again and again as i was running or executing the program the order of truth called and Foo const is changing somtimes it prints
truth called Foo const
and somtimes it prints
Foo const truth called
i am a bit confused here any time can this thing affect the output of other programs as well,if this kind of strategy is being used like somewhere i use System.err and somewhere System.out? will the sequence of output change?
please if you could highlight on this point it will be kinda of you.
Bharat Makwana
Ranch Hand
Joined: May 21, 2007
Posts: 107
posted
0
hi marc.
I have run this code many times on window.
When I use 'err' and 'out' then output is not predictable but when I use 'out' in both places output is predictable i.e truth called Foo Const
Originally posted by Bharat Makwana: ... When I use 'err' and 'out' then output is not predictable but when I use 'out' in both places output is predictable...
It seems like there's some multithreading going on, but I just don't know. I couldn't find anything on this last night. I'll do some more looking.
Ernest Friedman-Hill
author and iconoclast
Marshal
I suspect it's just the OS doing its thing. In traditional stdio implementations, stdout is line-buffered, and stderr is unbuffered, so the "dwell time" for stuff you write is going to be different -- i.e., writing newline to stderr isn't necessarily going to flush the data to the screen, but for stdout, that might very well be the case.
My prediction is that if you add
System.err.flush()
before returning from truth(), you'll see "truth called" printed first every single time.
I've been on at least one system where flush() didn't seem to fix this problem. You may need to write everything to the same stream. E.g. replace each System.err with System.out. Or call
System.setErr(System.getOut());
at the beginning of your program. Or use the command line to redirect error to standard out. This seems to work on both Windows and Unix-based systems (those I've tried, anyway).
Roughly, this means send file descriptor 2 (the error stream) to the same place file descriptor 1 (standard out) is pointing to.
"I'm not back." - Bill Harding, Twister
dhwani mathur
Ranch Hand
Joined: May 08, 2007
Posts: 621
posted
0
well!! Mr Ernest
i tried using System.err.flush(); but it is still giving the same output as explained by me earlier. Please can you tell me any reason for not printing the output in the same order? it will be kinda of you.
ok!!! i am using windows XP operating system and java 1.6 version i am using....
Ashutosh Limaye
Ranch Hand
Joined: Oct 24, 2005
Posts: 58
posted
0
I think what EFH is saying could be the reason.
According to my knowledge....
Normally err and out point to the same output device the screen.(you can change it though)
Now the both are buffers internally(for obvious reason of abstracting the output device from user programs)....usually the buffers are dumped when they are full...OS is conservative and does not output every byte that is written to the buffer...I think this might have to do something with the problem that our friend is facing...