Wonder if posting some debug print messages and also a Thread.activeCount() helps in understanding the flow of the program.
Here's the modified code & a sample output.
Please be aware that there may be other outputs that are not predictable.
===========================================================================
public class joining{
static Thread createThread(final int i,final Thread t1){
Thread t2=new Thread(){
public void run(){
System.out.println(i+1);
System.out.println("Inside anonymous run "+ this.getName());
try{
t1.join();
System.out.println("After join:Active Count " + Thread.activeCount());
}
catch(InterruptedException e){}
System.out.println(i+2);
System.out.println("After run "+ this.getName());
}
};
System.out.println(i+3);
System.out.println("After thread declaration Active Count "+ Thread.activeCount());
t2.start();
System.out.println(i+4);
System.out.println("Before Return Active Count " + Thread.activeCount());
return t2;
}
public static void main(String[] args){
createThread(10,createThread(20,Thread.currentThread()));
System.out.println("Active Count in main" + Thread.activeCount());
}
}
==========================================================================
TWO SAMPLE OUTPUTS:-
==================
C:\Amieya\CODES\j2ee>
java joining
23
After thread declaration 1
24
Before Return Active Count 2
13
After thread declaration 2
14
Before Return Active Count 3
Active Count in main3
21
Inside anonymous run Thread-0
After join:Active Count 3
22
After run Thread-0
11
Inside anonymous run Thread-1
After join:Active Count 2
12
After run Thread-1
C:\Amieya\CODES\j2ee>java joining
23
After thread declaration 1
24
Before Return Active Count 2
13
After thread declaration 2
14
Before Return Active Count 3
21
11
Inside anonymous run Thread-0
Inside anonymous run Thread-1
Active Count in main3
After join:Active Count 2
22
After run Thread-0
After join:Active Count 1
12
After run Thread-1
C:\Amieya\CODES\j2ee>