即使我先将调用开始,为什么线程最后执行?

时间:2019-12-07 13:44:41

标签: java multithreading

class test1 extends Thread  
{ 
   public void run() 
   { 
       System.out.println("Run method executed by child Thread"); 
   } 
   public static void main(String[] args) 
   { 
       test1 t = new test1(); 
       t.start();
       System.out.println(726*656); 
       System.out.println("Main method executed by main thread"); 
   } 
}


输出是-

476256

Main method executed by main thread
Run method executed by child Thread

为什么即使我先调用start方法,线程语句也最后出现

1 个答案:

答案 0 :(得分:1)

调用start时,新线程开始执行,但不会影响已经在执行的Main线程。如果您希望主线程等待新线程,则可以在新线程上调用“ join”之类的方法以等待其完成