I have problem understanding the meaning of join in the following case:
Thread t1=new MyThread(1);
Thread t2=new MyThread(2);
t1.start();
t2.start();
t1.join();
t2.join();
the question is: as I understand start calls the run method of the Thread. so the main thread first goes over the starts and thus all threads are initialized. then the main thread goes to t1.join and that means that the main thread will wait till t1 to end his running and only then continues to t2.join(). What I don't understand is the t1,t2 meanwhile already started so they are inside the run method. and thus the result supposed to be unexpected. am I right? so why does it come out in order: I have the following run method:
void run(){
for(int i=0; i<4; i++){
System.out.println(j);
}
}
when j is the identifier in the MyThread constructor; the output is:
2 2 2 2 1 1 1 1
or 1 1 1 1 2 2 2 2