Look main is also a thread so it is possible that it gets executed before other thread.
example
class xyz extends Thread
{
String name;
public xyz(String s)
{
name=s;
}
public void run()
{
System.out.print(name);
}
public static void main(String args[]) throws Exception
{
Thread t=new xyz("First");
Thread t1=new xyz("Second");
t.start(); // 1
// here
t1.start(); // 2
// some
System.out.print("Third");
}
}
program prints three words First Second Third but
order is not guaranteed for First Second Third,
because when you start t and t1 main thread is also running it is possible that main thread runs and completes before t or t1 and printing
Third First Second or Third Second First (depending on which t ot t1 runs first).
but if you want to be sure that it always prints First Second Third in that order do following
replace // here with t.join();
replace // some with t1.join();
Output is FirstSecondThird // guaranteed always
join is join thread that executes this code (main here) to end and so when you say t.join() Thread t will complete first and only than main will start.
now first t thread will run to completion printing First than t1 Seond and main thread runs Third will be printed last always.
[ September 12, 2005: Message edited by: Santana Iyer ]