Originally posted by Jacky Zhang:
option F
public void run() { synchronized(System.out) { write(); } }
I don't understand how can you just synchronize on System.out to make the write method always print XY together
public void write() {
System.out.print("X"); //line 1
System.out.print("Y"); //line 2
}
Finally figured it out, context was synchronized in System.out object, since the out object is declared as static, only one instance exists. That is, write() method is under that synchronized context, only when one thread finishes its execution on that context, that's the time other thread can use the System.out object.
Hope this helps.