Firstly, the output that you have got may vary from one run to another.
The only thing you are insuring using join is that the line:
is executed after the
thread passed in createThread() has finished running.
However, the value of "i" when the above is executed still depends on how many System.out. statements have been executed by then i.e. how many times "i" has been incremented.
I modified the program a little as follows:
and got the following output:
This will give you a better understanding.