I have this code:
System.err.print("number of terms = "); System.out.println(allTerms.size()); System.err.print("number of documents = "); System.out.print("number of documents = ");
I think the results should be like this:
number of terms = 10 number of documents =1000
but the results were
10 1000 number of terms = number of documents =
Why and how to solve it?
To solve the problem
System.out.print("number of terms = "); System.out.println(allTerms.size()); System.out.print("number of documents = "); System.out.print("number of documents = ");
System.out.println -> Sends output to standard output. Usually a monitor.
System.err.println -> Sends output to the standard error stream. Usually a monitor.
The out and err streams are independent.
To get the desired result, you must clear the threads or just use only one thread for all outputs.
System.out.print ("He"); System.out.print ("llo "); System.out.println ("World");
"Hello World",
System.out.print ("He"); System.err.print ("llo "); System.out.println ("World");
"llo He World" "HeWorld llo". 2 .
System.err.print() stderr, . System.out.print()
System.err.print()
System.out.print()
System.err System.ou - . System.out stdout system.err, stderr.
System.err System.out, , .
:
System.err.print("number of terms = ");
System.out.print("number of terms = ");
println, : System.out.println?
print * . .
System.err.print("number of terms = "); System.err.flush(); System.out.println(allTerms.size()); System.out.flush(); System.err.print("number of documents = "); System.err.flush(); System.out.print( numberOfDocuments ); System.out.flush();
number of terms = 10 number of documents = 1000
, , , , err .
err
This may be the reason: errand outthey have different output streams that will be printed according to the sequence of access to the console.
out