Thread與runnable在java中有什么不同

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

成都創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供定結(jié)網(wǎng)站建設(shè)、定結(jié)做網(wǎng)站、定結(jié)網(wǎng)站設(shè)計(jì)、定結(jié)網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、定結(jié)企業(yè)網(wǎng)站模板建站服務(wù),十余年定結(jié)做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。

java中實(shí)現(xiàn)多線程的方法有兩種:繼承Thread類和實(shí)現(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(),這樣寫只用一個(gè)主線程。多線程就失去了意義,所以應(yīng)該用start()方法來啟動(dòng)線程,start()方法會(huì)自動(dòng)調(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()方法啟動(dòng)一個(gè)新的線程。這樣不管th2.start()調(diào)用的run()方法是否執(zhí)行完,都繼續(xù)執(zhí)行th3.start()如果下面有別的代碼也同樣不需要等待th3.start()執(zhí)行完成,而繼續(xù)執(zhí)行。(輸出的線程id是無規(guī)則交替輸出的) 

2,實(shí)現(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í)行,程序只用一個(gè)線程。要多線程的目的,也要通過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é):實(shí)現(xiàn)java多線程的2種方式,runable是接口,thread是類,runnable只提供一個(gè)run方法,建議使用runable實(shí)現(xiàn) java多線程,不管如何,最終都需要通過thread.start()來使線程處于可運(yùn)行狀態(tài)。

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

當(dāng)前標(biāo)題:Thread與runnable在java中有什么不同
瀏覽地址:http://muchs.cn/article34/isjsse.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、微信小程序、做網(wǎng)站、網(wǎng)站設(shè)計(jì)公司、網(wǎng)站收錄、搜索引擎優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作