class A extends
Thread {
String[] sa;
public A(String[] sa) {
this.sa = sa;
}
public void run() {
synchronized (sa) {
System.out.print(sa[0] + sa[1] + sa[2]);
}
}
}
class B {
private static String[] sa = new String[]{"X","Y","Z"};
public static void main (String[] args) {
synchronized (sa) {
Thread t1 = new A(sa);
t1.start();
sa[0] = "A";
sa[1] = "B";
sa[2] = "C";
}
}
}
What are the possible results of attempting to compile and run the program?
a. Prints: XYZ.
b. Prints: AYZ.
c. Prints: ABZ.
d. Prints: ABC.
e. Compiler error.
f. Run time error.
g. None of the above.
Correct answer is d: ABC.
My confusion is that in Class B the main() method is synchronizing on String[] sa, which is declared in Class B. And in Class A is the run() method synching up on 'array object sa'(which is declared in Class A) that is diff. from 'String[] sa' in Class B? If thats the case then run() method does not have to wait for the lock release by the main() in Class B?
Thanks,
Sumeer.