java面試題之Thread的run()和start()方法有什么區別


run()方法:

  是在主線程中執行方法,和調用普通方法一樣;(按順序執行,同步執行)

start()方法:

  是創建了新的線程,在新的線程中執行;(異步執行)

 

public class App {
    public static void main( String[] args ){
        Thread thread = new Thread(){
          public void run(){
              test2();
          }
        };
//        thread.start();//會按照順序去執行線程,運行結果:test1;test2
        thread.run();//只要cpu有空閑的線程就可以運行該線程 ,運行結果:test2;test1
        System.out.println("test1");
    }
    static void test2(){
        System.out.println("test2");
    }
}

 


免責聲明!

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



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