I know thread synchronization. But in this code from the book Java Network Programming by Merlin Hughes, it is written that the println
method synchronizes on System.out
. I don’t understand how a method could be synchronized on System.out
.
Second question I wanna ask: Is the println
function an overridden method or is it just a user defined method in this code?
import java.io.*;
public class SimpleOut {
public static void main(String[] args) throws IOException {
for (int i = 0; i < args.length; i++) {
println (args[i]);
}
}
public static void println(String msg) throws IOException {
synchronized (System.out) {
for (int i=0 ; i<msg.length(); i++) {
System.out.write(msg.charAt (i) & 0xff);
}
System.out.write('\n');
}
System.out.flush();
}
}