Can someone explain me what's happening?
In your first example, when you
test the program, the first thread executes the entire main method before the second thread executes the run method.
*Occasionally* the virtual machine *might* decide to preempt the first thread and let the second thread execute. The virtual machine might preempt the first thread after it has executed only a few lines of the main method.
In your second example, when you test the program, the first thread invokes the sleep method. The thread does not sleep, but the virtual machine decides to preempt that thread and let the second thread execute.
When I play with Dan�s thread examples, I add Thread.yield(); at various places in the code to encourage the virtual machine to preempt the currently executing thread and let the other thread run. That way I can see more than one possible way the virtual machine might schedule the threads.
I also sometimes add print statements, to see the order the threads execute, although the I/O might also affect the thread scheduling.