Hi friends;
see following code
public class
Test extends Thread{
public
String g="";
public static void main(String argv[]){
Test r = new Test("one");
Test a=new Test("two");
Thread t = new Thread(r);
Thread t1 = new Thread(a);
t.start();
t1.start();
}
Test(String s){
g=s;
}
public synchronized void run(){
for(int i=0;i<2;i++){
try{
sleep(1000);
}
catch(InterruptedException e){}
System.out.println(g);
}
}
}
I am getting following as o/ps at different time, for above code.
1>two
one
two
one
2>one
two
one
two
why so? the code is compiling perfectly.
Thread of 'one' is starting 1st then how this o/p is getting
executed ?s it bcoz both start() methods are together???(one below the other)
but this can't be the ans?
Please can any one help me?
Thanks.
Shubha.