class B extends Thread{
static String s[] = new String[]{"x","y","z"};
public void run(){
System.out.println(s[0]+s[1]+s[2]);
}
public static void main(String ar[]){
new B().start();
s[0]= "A";
s[1]="B";
s[2]="C";
}
}
i thought the answer is going to be xyz,ayz,abz,abc
how come that happen the answer should be xyz because i think it's not going to print ABC caz its below the new B().start();
if i place it over the new B().start(); the output is going to be ABC why is that?
and what will happen if i place synchronize like this
class B extends Thread{
static String s[] = new String[]{"x","y","z"};
public "synchronized" void run(){
System.out.println(s[0]+s[1]+s[2]);
}
public static void main(String ar[]){
new B().start();
s[0]= "A";
s[1]="B";
s[2]="C";
}
}
i think the answer is still going to be same. why is that?
check this one
class B extends Thread{
static String s[] = new String[]{"x","y","z"};
static B a = new B();
static B b = new B();
public void run(){
synchronized(a){
System.out.println(s[0]+s[1]+s[2]);
}
synchronized(b){
s[0] = "A";
s[1] = "B";
s[2] = "C";
}
}
public static void main(String ar[]){
// new B().start();
// B b = new B();
a.start();
b.start();
}
}
this is giving xyz,xyz why is that
[ October 13, 2007: Message edited by: Burkhard Hassel ]