更好的理解java多線程

來源:互聯網
上載者:User

標籤:java多線程   繼承   public   cep   interrupt   傳遞   情況   base   父類   

1.線程的建立

之前知道Java中一個多線程的建立方法是繼承Thread類或者實現Runable介面,但是看不懂下面這種建立線程的方法

第一種

 

[java] view plain copy  print?
  1. new Thread(new Runnable() {  
  2.             @Override  
  3.             public void run() {  
  4.               
  5.                 }  
  6.             }  
  7.         }).start();//這種方式中new Thread()是建立了一個線程,而new Runable()對象中是線程想要執行的代碼,這樣把想要執行的代碼放到一個建立的對象中,所  
[java] view plain copy  print?
  1. <span style="white-space:pre">          </span>   //以很好的體現了物件導向的編程規則。  


或者下面這種建立格式

 

第二種

 

[java] view plain copy  print?
  1. new Thread(){  
  2.         @Override  
  3.         public void run() {  
  4.             // TODO Auto-generated method stub  
  5.             super.run();  
  6.         }  
  7.     }.start();  


其實兩種方法昨天看了一段是視頻現在理解了,其實第一種方法就是物件導向的思維模式去建立一個線程,在Thread的源碼中,一種是通過構造方法傳遞一個Runable對象然後為target賦值,然後去實現一個線程的建立,也就是上面的第一種建立方法,一種是直接重寫run方法,也就是上面的第二種方法建立線程,但是網上通常是使用第一種方法的比較多?那是為什麼呢?是因為第一種通過構造方法的方式建立線程更能體現面向的規則,new Thread()是建立了一個對象,這個對象想要執行的代碼就寫在了括弧裡面,這樣就很好的體現了物件導向的編程方式。

 

那我們想一下,下面這種建立對象的方式是會執行Runable中的run方法還是執行thread的run方法呢?

 

[java] view plain copy  print?
  1. new Thread(  
  2.                 new  Runnable() {  
  3.                     public void run() {  
  4.                         while (true) {  
  5.                             try {  
  6.                                 Thread.sleep(500);  
  7.                             } catch (InterruptedException e) {  
  8.                                 e.printStackTrace();  
  9.                             }  
  10.                             System.out.println("runable" + Thread.currentThread().getName());  
  11.                         }  
  12.                     }  
  13.                 }  
  14.         ) {  
  15.             @Override  
  16.             public void run() {  
  17.                 while (true) {  
  18.                     try {  
  19.                         Thread.sleep(500);  
  20.                     } catch (InterruptedException e) {  
  21.                         e.printStackTrace();  
  22.                     }  
  23.                     System.out.println("thread" + Thread.currentThread().getName());  
  24.                 }  
  25.             }  
  26.         }.start();  


new Thread(new Runable(){}){}.start();答案是執行thread中的run方法(即是子類中的run方法),為什麼呢?因為建立一個new thread的時候重寫了父類中的run()方法,那麼就會直接執行thread中的run方法了,如果這個子類中的run方法不存在的情況下才會去執行父類中的run方法。

更好的理解java多線程

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.