java 線程的run和start方法的不同


  1. public class TestThread1 {  
  2.     public static void main(String[] args) {  
  3.         Runner1 r = new Runner1();  
  4. //       r.run();//這是方法調用,而不是開啓一個線程  
  5.         Thread t = new Thread(r);// 調用了Thread(Runnable target)方法。且父類對象變量指向子類對象。  
  6.         t.start();  
  7.         for (int i = 0; i < 100000; i++) {  
  8.              if(i%10000==0){  
  9.                  System.out.println("main");  
  10.              }  
  11.         }  
  12.     }  
  13.     /* 
  14.      * run的運行結果: 
  15.      * 先顯示10個thread,再來上10個main, 
  16.      * 表示使用run方法調用的時候相當於一般的過程調用,並沒有加入到線程中區 
  17.      * start的運行結果: 
  18.      * thread和main是穿插進行的,就表示該方法已經加入了線程中去 
  19.      */  
  20. }  
  21. class Runner1 extends Thread {   
  22.     public void run() {  
  23.         for (int i = 0; i < 100000; i++) {  
  24.              if(i%10000==0){  
  25.                  System.out.println("thread");  
  26.              }  
  27.         }  
  28.     }  
  29. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章