溫馨提示×

溫馨提示×

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

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

Thread與runnable在java中有什么不同

發(fā)布時間:2020-12-02 16:27:05 來源:億速云 閱讀:147 作者:Leah 欄目:編程語言

Thread與runnable在java中有什么不同 ?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。

java中實現(xiàn)多線程的方法有兩種:繼承Thread類和實現(xiàn)runnable接口

1,繼承Thread類,重寫父類run()方法

  public class thread1 extends Thread {
 
    public void run() {
        for (int i = 0; i < 10000; i++) {
            System.out.println("我是線程"+this.getId());
        }
    }
 
    public static void main(String[] args) {
        thread1 th2 = new thread1();
        thread1 th3 = new thread1();
        th2.run();
        th3.run();
    }
   } 

run()方法只是普通的方法,是順序執(zhí)行的,即th2.run()執(zhí)行完成后才執(zhí)行th3.run(),這樣寫只用一個主線程。多線程就失去了意義,所以應(yīng)該用start()方法來啟動線程,start()方法會自動調(diào)用run()方法。上述代碼改為:

 public class thread1 extends Thread {
    
    public void run() {
        for (int i = 0; i < 10000; i++) {
            System.out.println("我是線程"+this.getId());
        }
    }
 
    public static void main(String[] args) {
        thread1 th2 = new thread1();
        thread1 th3 = new thread1();
        th2.start();
        th3.start();
    }
}

通過start()方法啟動一個新的線程。這樣不管th2.start()調(diào)用的run()方法是否執(zhí)行完,都繼續(xù)執(zhí)行th3.start()如果下面有別的代碼也同樣不需要等待th3.start()執(zhí)行完成,而繼續(xù)執(zhí)行。(輸出的線程id是無規(guī)則交替輸出的) 

2,實現(xiàn)runnable接口

public class thread2 implements Runnable {
 
    public String ThreadName;
    
    public thread2(String tName){
        ThreadName = tName;
    }
    
    
    public void run() {
        for (int i = 0; i < 10000; i++) {
            System.out.println(ThreadName);
        }
    }
    
    public static void main(String[] args) {
        thread2 th2 = new thread2("線程A");
        thread2 th3 = new thread2("Thread-B");
        th2.run();
        th3.run();
    }
}

和Thread的run方法一樣Runnable的run只是普通方法,在main方法中th3.run()必須等待th2.run()執(zhí)行完成后才能執(zhí)行,程序只用一個線程。要多線程的目的,也要通過Thread的start()方法(runnable是沒有start方法)。上述代碼修改為:

public class thread2 implements Runnable {
 
    public String ThreadName;
    
    public thread2(String tName){
        ThreadName = tName;
    }
    
    
    public void run() {
        for (int i = 0; i < 10000; i++) {
            System.out.println(ThreadName);
        }
    }
    
    public static void main(String[] args) {
        thread2 th2 = new thread2("線程A");
        thread2 th3 = new thread2("Thread-B");
        Thread myth2 = new Thread(th2);
        Thread myth3 = new Thread(th3);
        myth2.start();
        myth3.start();
    }
}

 總結(jié):實現(xiàn)java多線程的2種方式,runable是接口,thread是類,runnable只提供一個run方法,建議使用runable實現(xiàn) java多線程,不管如何,最終都需要通過thread.start()來使線程處于可運行狀態(tài)。

看完上述內(nèi)容,你們掌握Thread與runnable在java中有什么不同 的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

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

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

AI