Threads are **** confusing.. pls help...

This code i was going through is very confusing. I have synchronized the String object sa which is in class B. I am then creating a thread of class A, and passing the object sa to class A. Only after having passed the object am i changing the value of String object in class B, how does it get reflected in class A's object. Whats happening here?

class Aextends Thread{

String[] sa;

public A(String[] sa){this.sa = sa;}

publicvoid run(){

synchronized (sa){System.out.print(sa[0] + sa[1] + sa[2]);}

}}

publicclass B{

publicstaticvoid main (String[] args){

String[] sa =new String[]{"X","Y","Z"};

synchronized (sa){

Thread t1 =new A(sa); t1.start();

sa[0] ="A"; sa[1] ="B"; sa[2] ="C";

}}}

[1822 byte] By [KartikVa] at [2007-10-1 5:48:17]
# 1
Well the output is actually ABC, while i was expecting XYZ ( **** expectations!!)
KartikVa at 2007-7-9 14:02:16 > top of Java-index,Java Essentials,Java Programming...
# 2
> Well the output is actually ABC, while i was> expecting XYZ ( **** expectations!!)Why were you expecting XYZ?
mlka at 2007-7-9 14:02:16 > top of Java-index,Java Essentials,Java Programming...
# 3

synchronized (sa) {

Thread t1 = new A(sa); t1.start();

sa[0] = "A"; sa[1] = "B"; sa[2] = "C";

}

In the above piece of code, am i not creating Thread t1and passing object sa to it. So doesnt that mean that the String array with values XYZ have been passed to t1 object. Only after passing the above string array with XYZ am i changing values in sa of class B to A,,B,C. I do understand that it is a synchronized block, so thread t1 will have to wait for this block to complete, but that does not mean that the thread t1 would not have been created with String XYZ and be ready to this synchronization block to complete. This is my understanding and am sure i am wrong somewhere coz the output is totally different from what i preceive.

Please let me know ...

Thanks.

KartikVa at 2007-7-9 14:02:16 > top of Java-index,Java Essentials,Java Programming...
# 4
I'm in a mean mood, so instead of just handing out the answer, I'll prod you for a bit...To start with, you are not passing a String, you are passing a to a refrence n array of refrences to Strings.
mlka at 2007-7-9 14:02:16 > top of Java-index,Java Essentials,Java Programming...
# 5

Aha..

THAT BIT OF PRODDING WILL DO.....

thanks :) (jhmm.. was expecting something complex and missed out on the basics)..

public class StringTest {

static void m1 (String a[]){

System.out.println("The values of a are " + a[0] + a[1]);

}

public static void main(String[] args) {

String s[] ={"a1","a2","a3"};

m1(s);

s[0]="3";

m1(s);

}

}

KartikVa at 2007-7-9 14:02:16 > top of Java-index,Java Essentials,Java Programming...
# 6

> This code i was going through is very confusing. I

> have synchronized the String object sa which is in

> class B. I am then creating a thread of class A, and

> passing the object sa to class A. Only after having

> passed the object am i changing the value of String

> object in class B, how does it get reflected in class

> A's object. Whats happening here?

>

class A extends Thread {

String[] sa;

public A(String[] sa) {this.sa = sa;}

public void run() {

/*

Here the the thread will wait until this thread obtain the monitor of sa

to do so it will have to wait until the main releases the monitor

but when main releases the monitor the value of sa is set to ABC

*/

synchronized (sa) {

// so this will print ABC

System.out.print(sa[0] + sa[1] a[1] + sa[2]);

}

}}

public class B {

public static void main (String[] args) {

String[] sa = new String[]{"X","Y","Z"};

/* Here you are obtaining the monitor of sa

and run method of other thread cant obtain the monitor

until this thread releases it */

synchronized (sa) {

Thread t1 = new A(sa); t1.start();

// Above line dosent mean that thread A get started immedeatly

//still the active thread is main

// so at the time when following line is executed

// the thread A may not even started its run

sa[0] = "A"; sa[1] = "B"; sa[2] = "C";

}

//this is where you release the monitior

// and then only your other thread can obtain the monitor of sa

// but its too late now since sa has values ABC

}}

LRMKa at 2007-7-9 14:02:16 > top of Java-index,Java Essentials,Java Programming...
# 7
> THAT BIT OF PRODDING WILL DO..... Dam it, you just don't have the prodding mentality one requires. There was a good hour or two of vague hints in that :D
mlka at 2007-7-9 14:02:16 > top of Java-index,Java Essentials,Java Programming...
# 8
haha.. i have a lot more questions to ponder on and lot more java to learn..will be back soon... hope ya help.. nedd more prodding...Thanks..
KartikVa at 2007-7-9 14:02:16 > top of Java-index,Java Essentials,Java Programming...
# 9

> haha.. i have a lot more questions to ponder on and

> lot more java to learn..

> will be back soon... hope ya help.. nedd more

> prodding...

> Thanks..

To understand threads and synchronization you may need to learn how to think in 4th dimention (Time)

LRMKa at 2007-7-9 14:02:16 > top of Java-index,Java Essentials,Java Programming...