Thread和Runnable的區(qū)別

線程Thread的5狀態(tài):
創(chuàng)建——>就緒——>運(yùn)行——>阻塞——>停止
創(chuàng)建:new
就緒:創(chuàng)建對(duì)象后,執(zhí)行start方法,即加入線程隊(duì)列等待獲取CPU資源。這個(gè)時(shí)候?yàn)榫途w狀態(tài)。
運(yùn)行:搶奪到CPU時(shí)間片,該thread開始運(yùn)行run中的代碼。
阻塞:如果在run方法中執(zhí)行了sleep或者調(diào)用了thread的wait/join方法,意味著放棄CPU資源而進(jìn)入阻塞狀態(tài),但是運(yùn)行還沒有完畢,等待重新獲得CPU資源后,重新進(jìn)入就緒狀態(tài)
停止:一般停止線程有兩種方式:1執(zhí)行完畢run方法,2調(diào)用stop方法,后者不推薦使用??梢栽趓un方法中循環(huán)檢查某個(gè)public變量,當(dāng)想要停止該線程時(shí)候,通過thread.para為false即可以將run提前運(yùn)行完畢,即進(jìn)入了停止?fàn)顟B(tài)


1. 關(guān)于線程創(chuàng)建問題

多線程可以通過兩種方式來創(chuàng)建。
一、通過繼承Thread類
二、通過實(shí)現(xiàn)Runnable接口
java:單繼承多實(shí)現(xiàn)

2. 多線程有哪些優(yōu)點(diǎn)呢?

一、多線程共享同一塊內(nèi)存空間和一組系統(tǒng)資源
二、線程本身的數(shù)據(jù)通常都是只有微處理器的寄存器數(shù)據(jù),以及供程序執(zhí)行的堆棧。所以系統(tǒng)在產(chǎn)生一個(gè)線程或者線程的切換要比進(jìn)程系統(tǒng)的負(fù)擔(dān)小很多。

3. 關(guān)于共享數(shù)據(jù)的問題比較

Thread:多個(gè)線程分別完成自己的任務(wù)
Runnable:多個(gè)線程共同完成一個(gè)任務(wù)。

Thread:
  1. 自定義一個(gè)類ThreadTest 繼承Thread
  2. new ThreadTest.start(); /可以創(chuàng)建多個(gè)ThreadTest實(shí)例并且啟動(dòng),/
public  class ThreadDemo   
{   
      public static void main(String []args)   
      {   
          MyThread thread1 = new MyThread ();   
          MyThread thread2 = new MyThread ()  
          MyThread thread3 = new MyThread ();   
          MyThread thread4 = new MyThread ();  
          thread1.start(); 
          thread2.start(); 
          thread3.start(); 
          thread4.start(); 
      }   
}   
class MyThread extends Thread
{ 
          int things = 5; 

          public void run() { 
              while(things > 0)
              { 
              System.out.println(currentThread().getName() + " things:" + things); things--; 
              } 
          }
}

結(jié)果如下:

Thread-0 things:5
Thread-0 things:4
Thread-2 things:5
Thread-1 things:5
Thread-1 things:4
Thread-1 things:3
Thread-1 things:2
Thread-1 things:1
Thread-2 things:4
Thread-2 things:3
Thread-2 things:2
Thread-2 things:1
Thread-0 things:3
Thread-0 things:2
Thread-0 things:1

-->運(yùn)行后會(huì)發(fā)現(xiàn),多個(gè)線程對(duì)象各自占有各自的資源,并且去爭奪CPU時(shí)間片去完成各自的線程,并不是同時(shí)完成統(tǒng)一的任務(wù),所以得出結(jié)論:Thread類實(shí)際無法達(dá)到資源共享的目的

Runnable:
  1. 自定義一個(gè)類ThreadTest實(shí)現(xiàn)Runnable接口
  2. new Thread(test).start();
    此處是new 一個(gè)線程畢竟傳入實(shí)現(xiàn)了Runnable的類實(shí)例作為參數(shù)
public  class ThreadDemo   
{   
      public static void main(String []args)   
      {   
          MyRunnable run = new MyRunnable("run");  
          Thread th1 = new Thread(run, "Thread 1");
          Thread th2 = new Thread(run, "Thread 2");  
          Thread th3 = new Thread(run, "Thread 3");

          th1.start();
          th2.start();
          th3.start();
    }   
}   
class MyRunnable implements Runnable   
{   
          String name;   
          public MyRunnable(String name) 
          { 
              this.name = name; 
          }

          int things = 5;
          public void run()   
          {   
            while(things >0)   
          {   
            System.out.println(name + " things:" + things);  
        }   
    }   
}  
run things:3
run things:2
run things:1
run things:0
run things:3

-->運(yùn)行后會(huì)發(fā)現(xiàn),運(yùn)行之后我們發(fā)現(xiàn),這三個(gè)線程使用同一資源同時(shí)(即合作)完成了我們需要完成的任務(wù)。,所以得出結(jié)論:Runnable是可以共享數(shù)據(jù)的,多個(gè)Thread可以同時(shí)加載一個(gè)Runnable,當(dāng)各自Thread獲得CPU時(shí)間片的時(shí)候開始運(yùn)行runnable,runnable里面的資源是被共享的

通過以上比較我們即可得出Thread與Runnable的區(qū)別:
1、Runnable適合于多個(gè)相同程序代碼線程去處理統(tǒng)一資源的情況,把虛擬的cpu(線程)同程序的代碼,數(shù)據(jù)有效分離,較好體現(xiàn)面向?qū)ο蟮木幊痰乃枷?
2、Runnable可以避免由于Java的單繼承機(jī)制帶來的局限??梢栽倮^承其他類的同時(shí),還能實(shí)現(xiàn)多線程的功能。
3、Runnable能增加程序的健壯性。代碼能夠被多個(gè)線程共享

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 本文主要講了java中多線程的使用方法、線程同步、線程數(shù)據(jù)傳遞、線程狀態(tài)及相應(yīng)的一些線程函數(shù)用法、概述等。 首先講...
    李欣陽閱讀 2,597評(píng)論 1 15
  • Java多線程學(xué)習(xí) [-] 一擴(kuò)展javalangThread類 二實(shí)現(xiàn)javalangRunnable接口 三T...
    影馳閱讀 3,108評(píng)論 1 18
  • Java中Runnable和Thread的區(qū)別分析 在java中可有兩種方式實(shí)現(xiàn)多線程,一種是繼承Thread類,...
    簡單應(yīng)用閱讀 986評(píng)論 0 6
  • 一、線程的生命周期 線程狀態(tài)轉(zhuǎn)換圖: 1、新建狀態(tài) 用new關(guān)鍵字和Thread類或其子類建立一個(gè)線程對(duì)象后,該線...
    我是嘻哈大哥閱讀 1,016評(píng)論 0 8
  • 交朋友: 不一定要交有錢有勢的, 但一定要交有情有義的; 不一定要交長得漂亮的, 但一定要交心地善良的。 交朋友:...
    人人秀閱讀 254評(píng)論 0 1

友情鏈接更多精彩內(nèi)容