I try to run the example from a book(Paul Hyde, Java Thread Programming). It says that the order of threads will interchange. But I always get : 10 "Main thread" prints and 10 "New thread" ones afterwards.
What is more interesting : if I will use tt.run instead of tt.start then result will be vice versa. Maybe the reason that the book is quite old and examples are base on JDK 1.2??? Code is below:
public class TwoThread extends Thread
{
public void run()
{
for (int i = 0; i < 10; i++)
{
System.out.println("New thread");
}
}
public static void main(String[] args)
{
TwoThread tt = new TwoThread();
tt.start();
for (int i = 0; i < 10; i++)
{
System.out.println("Main thread");
}
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…