溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

java中Runnable和Thread的區(qū)別有哪些

發(fā)布時(shí)間:2021-12-03 17:31:00 來源:億速云 閱讀:115 作者:小新 欄目:編程語言

這篇文章將為大家詳細(xì)講解有關(guān)java中Runnable和Thread的區(qū)別有哪些,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

在java中可有兩種方式實(shí)現(xiàn)多線程,一種是繼承Thread類,一種是實(shí)現(xiàn)Runnable接口;Thread類是在java.lang包中定義的。一個(gè)類只要繼承了Thread類同時(shí)覆寫了本類中的run()方法就可以實(shí)現(xiàn)多線程操作了,但是一個(gè)類只能繼承一個(gè)父類,這是此方法的局限,

  下面看例子:

  package org.thread.demo;
  class MyThread extends Thread{
  private String name;
  public MyThread(String name) {
  super();
  this.name = name;
  }

  public void run(){
  for(int i=0;i<10;i++){
  System.out.println("線程開始:"+this.name+",i="+i);
  }
  }
  }

  package org.thread.demo;

  public class ThreadDemo01 {
  public static void main(String[] args) {
  MyThread mt1=new MyThread("線程a");
  MyThread mt2=new MyThread("線程b");
  mt1.run();
  mt2.run();
  }
  }

  但是,此時(shí)結(jié)果很有規(guī)律,先第一個(gè)對(duì)象執(zhí)行,然后第二個(gè)對(duì)象執(zhí)行,并沒有相互運(yùn)行。在JDK的文檔中可以發(fā)現(xiàn),一旦調(diào)用start()方法,則會(huì)通過JVM找到run()方法。下面啟動(dòng)

  start()方法啟動(dòng)線程:

  package org.thread.demo;

  public class ThreadDemo01 {
  public static void main(String[] args) {
  MyThread mt1=new MyThread("線程a");
  MyThread mt2=new MyThread("線程b");
  mt1.start();
  mt2.start();
  }
  };
      這樣程序可以正常完成交互式運(yùn)行。那么為啥非要使用start();方法啟動(dòng)多線程呢?

  在JDK的安裝路徑下,src.zip是全部的java源程序,通過此代碼找到Thread中的start()方法的定義,可以發(fā)現(xiàn)此方法中使用了private native void start0();其中native關(guān)鍵字表示可以調(diào)用操作系統(tǒng)的底層函數(shù),那么這樣的技術(shù)成為JNI技術(shù)(java Native Interface)

  ·Runnable接口
  在實(shí)際開發(fā)中一個(gè)多線程的操作很少使用Thread類,而是通過Runnable接口完成。
  public interface Runnable{

  public void run();
  }

  例子:

  package org.runnable.demo;

  class MyThread implements Runnable{
  private String name;
  public MyThread(String name) {
  this.name = name;
  }

  public void run(){

  for(int i=0;i<100;i++){
  System.out.println("線程開始:"+this.name+",i="+i);
  }
  }
  };

  但是在使用Runnable定義的子類中沒有start()方法,只有Thread類中才有。此時(shí)觀察Thread類,有一個(gè)構(gòu)造方法:public Thread(Runnable targer)此構(gòu)造方法接受Runnable的子類實(shí)例,也就是說可以通過Thread類來啟動(dòng)Runnable實(shí)現(xiàn)的多線程。(start()可以協(xié)調(diào)系統(tǒng)的資源):

  package org.runnable.demo;
  import org.runnable.demo.MyThread;

  public class ThreadDemo01 {
  public static void main(String[] args) {
  MyThread mt1=new MyThread("線程a");
  MyThread mt2=new MyThread("線程b");
  new Thread(mt1).start();
  new Thread(mt2).start();
  }
  }

  · 兩種實(shí)現(xiàn)方式的區(qū)別和聯(lián)系:
  在程序開發(fā)中只要是多線程肯定永遠(yuǎn)以實(shí)現(xiàn)Runnable接口為主,因?yàn)閷?shí)現(xiàn)Runnable接口相比
  繼承Thread類有如下好處:
  ->避免點(diǎn)繼承的局限,一個(gè)類可以繼承多個(gè)接口。
  ->適合于資源的共享
  以賣票程序?yàn)槔?,通過Thread類完成:

  package org.demo.dff;
  class MyThread extends Thread{
  private int ticket=10;
  public void run(){
  for(int i=0;i<20;i++){
  if(this.ticket>0){
  System.out.println("賣票:ticket"+this.ticket--);
  }
  }
  }
  };

  下面通過三個(gè)線程對(duì)象,同時(shí)賣票:
  package org.demo.dff;
  public class ThreadTicket {
  public static void main(String[] args) {
  MyThread mt1=new MyThread();
  MyThread mt2=new MyThread();
  MyThread mt3=new MyThread();
  mt1.start();//每個(gè)線程都各賣了10張,共賣了30張票
  mt2.start();//但實(shí)際只有10張票,每個(gè)線程都賣自己的票
  mt3.start();//沒有達(dá)到資源共享
  }
  }

  如果用Runnable就可以實(shí)現(xiàn)資源共享,下面看例子:

  package org.demo.runnable;
  class MyThread implements Runnable{
  private int ticket=10;
  public void run(){
  for(int i=0;i<20;i++){
  if(this.ticket>0){
  System.out.println("賣票:ticket"+this.ticket--);
  }
  }
  }
  }

  package org.demo.runnable;
  public class RunnableTicket {
  public static void main(String[] args) {
  MyThread mt=new MyThread();
  new Thread(mt).start();//同一個(gè)mt,但是在Thread中就不可以,如果用同一
  new Thread(mt).start();//個(gè)實(shí)例化對(duì)象mt,就會(huì)出現(xiàn)異常
  new Thread(mt).start();
  }
  };


  雖然現(xiàn)在程序中有三個(gè)線程,但是一共賣了10張票,也就是說使用Runnable實(shí)現(xiàn)多線程可以達(dá)到資源共享目的。
  Runnable接口和Thread之間的聯(lián)系:
  public class Thread extends Object implements Runnable
  發(fā)現(xiàn)Thread類也是Runnable接口的子類。

關(guān)于“java中Runnable和Thread的區(qū)別有哪些”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI