每天一道Java題[10]

來源:互聯網
上載者:User

標籤:int   static   ide   void   需要   creation   實現   抽象   .com   

題目

闡述建立線程最常用的兩種方法及其對比。

   

解答

方法一:繼承Thread類實現

步驟:

  1. 建立Thread類的子類,如MyThread。
  2. 重寫Thread類的run()方法。
  3. 執行個體化MyThread類,對象名如myThread。
  4. 運用Thread類的start()方法啟動線程,如myThread.start()。

   

方法二:實現Runnable介面

步驟:

  1. 建立一個類,如MyRunnableThread,實現Runnable介面。
  2. 建立MyRunnableThread類的對象。
  3. 執行個體化Thread類,對象名如thread,並向其建構函式傳入MyRunnableThread類和線程名兩個參數。
  4. 運用Thread類的start()方法啟動線程,如thread.start()。

   

繼承Thread類建立線程與實現Runnable介面建立線程的不同之處在於,當用同一個類建立多個線程的時候,前者實際上是建立了多個不同的Thread對象,它內部的run()方法執行的時候是在各自對象中執行,互不干擾,如同多個線程執行多個任務;而後者實際上是使用同一個對象來建立多個線程,所以對象內的屬性會公用,那就相當於多個線程在執行同一個任務一樣。

另外,在使用上,繼承了Thread類的類就不能繼承其它類了,而實現了Runnable介面的類,還可以繼承其他類,前者相對局限。

這樣子說法可能有點抽象,下面那用代碼講解一下。

   

參考代碼

   

MyThread類

package me.huangzijian;public class MyThread extends Thread {    private int num = 10;    private String name;    public MyThread(String name) {        this.name = name;    }    @Override    public void run() {        int count = num;        for (int i = 0; i < count; i++) {            System.out.println(this.name + ":" + num);            num--;        }    }}

   

MyRunnable類

package me.huangzijian;public class MyRunnableThread implements Runnable {    private int num = 10;    @Override    public void run() {        int count = num;        for (int i = 0; i < count; i++) {            System.out.println(Thread.currentThread().getName() + ":" + num);            num--;        }    }}

 

 

TheadCreation類

package me.huangzijian;public class ThreadCreation {    public static void main(String[] args) {        // 繼承Thread類實現        MyThread myThread1 = new MyThread("MyThread1");        MyThread myThread2 = new MyThread("MyThread2");        MyThread myThread3 = new MyThread("MyThread3");        myThread1.start();        myThread2.start();        myThread3.start();        // 實現Runnable介面        MyRunnableThread myRunnableThread = new MyRunnableThread();        Thread t1 = new Thread(myRunnableThread, "MyRunnableThread1");        Thread t2 = new Thread(myRunnableThread, "MyRunnableThread2");        Thread t3 = new Thread(myRunnableThread, "MyRunnableThread3");        t1.start();        t2.start();        t3.start();    }}

 

運行結果:

運行ThreadCreation後,我們會看到,繼承Thread類實現的結果如下:

可以看到,三個線程對於欄位num的操作是各操作各的,都將num從10減到1。

而實現Runnable介面的就誒過如下:

可以看到,三個線程對同一個myRunnableThread對象的num進行共同的操作。根據程式,每個線程迴圈10次,所以num從10一直減到負數。有朋友可能會問,為什麼一開始三個線程都會擷取到10,這就是線程同步的問題了,需要用到synchronized等關鍵字進行修飾。

   

  

每天一道Java題[10]

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.