The following code when run
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";
}}}
produces the output "ABC"
The explanation given for this says as follows :
"The block inside the main method is synchronized on the String array object sa. Inside the block, a new thread t1 is started and will run at the discretion of the thread scheduler. The A.run method also contains a block that is synchronized on the String array object sa. Even if the thread scheduler moves thread t1 into the Running state, it will block while attempting to acquire the lock of the String array object sa. Thread t1 will continue to block until the synchronized block in the B.main method runs to completion. At that time, the contents of the String array object have all been updated."
But i didnt understand any thing from this.
Could someone please explain me in detail on how this happens ?
Thanks in advance. Srinu