main入口方法中創建線程執行順序的問題


 
           
 1     public static void main(String args[]) {
 2 
 3         
 4         Thread t1=new Thread(){
 5             public void run(){
 6                 System.out.println("1");
 7             }
 8         };
 9         Thread t2=new Thread(){
10             public void run(){
11                 System.out.println("2");
12             }
13         };
14         t1.run();
15         t2.start();
16         
17         System.out.println("3");
18         
19 
20         
21         
22     }

 

輸出結果:

 

 

start()方法來啟動線程,真正實現了多線程運行,main為主線程,所以3先於2輸出

run()方法當作普通方法的方式調用,程序還是要順序執行,程序執行路徑還是就一條,並沒有達到寫線程的目的。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM