Given:
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();
}
}
We want to guarantee that the output can be either XXYY or YYXX, but never XYXY or any other combination. Which of the following method definitions could be added to the Letters class to make this guarantee? (Choose all that apply.)
public void run() { write(); }
public synchronized void run() { write(); }
public static synchronized void run() { write(); }
public void run() { synchronized(this) { write(); } } public void run() { synchronized(Letters.class) { write(); } }
public void run () { synchronized (System.out) { write (); } }
public void run() { synchronized(System.out.class) { write(); } }
---------------------------------------------------
my understanding(and
testing in my JVM) says that option D is also correct, though i am still doubtful about two answers named as E and F. At first i thought that only option D is correct. Can any body elaborate in detail