please look at this class, static method calls and output.
public class OneThreadManyStaticCalls {
public static final Calculator calculator = new Calculator();
public static void main(String[] args) {
dummy(0, 1, 1);
dummy(0, 2, 2);
dummy(0, 3, 5);
dummy(0, 4, 44);
dummy(0, 5, 5);
}
public static void dummy(int a, int b, int expected) {
System.out.print(System.currentTimeMillis() + "\t");
if (calculator.add(a, b) == expected) {
System.out.println("OK");
} else {
System.err.println("NOK");
}
}
}
I got diffrent (order from System.out.print) ouputs running this program. Example:
NOK
NOK
1342527389506 OK
1342527389506 OK
1342527389506 1342527389506 1342527389506 OK
Could any of you explain me (with details) why?
Thanks in advance.
sznury
System.err and System.out are two different streams which are interleaved in your console window – they’re not necessarily synchronized.
Try using(nevermind, this doesn’t seem to work) to force the output to be handled, or print all your output to the same stream.System.*.flush()Gives this result