Hi, I have a question about synchronization of statics. I'm a bit confused. The problem is the following (from K&B)
public class Letters extends
Thread {
private
String name;
public Letters(String name) { this.name = name; }
public void write()
System.out.print(name);
System.out.print(name);
}
public static void main(String[] args) {
new Letters("X").start();
new Letters("Y").start();
}
}
And the options are:
A. public void run() { write(); }
B. public synchronized void run() { write(); }
C. public static synchronized void run() { write(); }
D. public void run() { synchronized(this) { write(); } }
E. public void run() { synchronized(Letters.class) { write(); } }
F. public void run() { synchronized(System.out) { write(); } }
G. public void run() { synchronized(System.out.class) { write(); } }
The answers in the book are E & F, which I could corroborate by running the code... But I'm confused about option E.
As far as I know, the expression synchronized(Letters.class) synchronizes the static methods of the class. But since the methoid write is not static, the way is it forbidden that it could be run by two different threads simultaneously?
Thanks in advance
XM