java創建線程的三種方式及其對比


一、繼承Thread類創建線程類

(1)定義Thread類的子類,並重寫該類的run方法,該run方法的方法體就代表了線程要完成的任務。因此把run()方法稱為執行體。

(2)創建Thread子類的實例,即創建了線程對象。

(3)調用線程對象的start()方法來啟動該線程。

public class Main {            
      public static void main(String[] args) throws Exception {     
          myThread th1 = new myThread();
          myThread th2 = new myThread();
          th1.start();
          th2.start();
     }           
}

class myThread extends Thread {    
    public void run() {
        for(int i = 0; i < 5; i++) {
            System.out.println(Thread.currentThread().getName() + "運行 : " + i );
        }
    }
}

 

二、通過Runnable接口創建線程類

(1)定義runnable接口的實現類,並重寫該接口的run()方法,該run()方法的方法體同樣是該線程的線程執行體。

(2)創建 Runnable實現類的實例,並依此實例作為Thread的target來創建Thread對象,該Thread對象才是真正的線程對象。

(3)調用線程對象的start()方法來啟動該線程。

事實上,當傳入一個Runnable target參數給Thread后,Thread的run()方法就會調用target.run(),參考JDK源代碼:
public void run() {
  if (target != null) {
   target.run();
  }
}

示例代碼為:

public class Main {            
      public static void main(String[] args) throws Exception {   
          myThread myth = new myThread();
          Thread th1 = new Thread(myth);
          Thread th2 = new Thread(myth);
          th1.start();
          th2.start();
     }           
}

class myThread implements Runnable {
    
    public void run() {
        for(int i = 0; i < 5; i++) {
            System.out.println(Thread.currentThread().getName() + "運行 : " + i );
        }
    }
}

 

 

 

三、通過Callable和Future創建線程

(1)創建Callable接口的實現類,並實現call()方法,該call()方法將作為線程執行體,並且有返回值。

(2)創建實現了Callable接口的實現類的實例,使用FutureTask類來包裝Callable對象,該FutureTask對象封裝了該Callable對象的call()方法的返回值。

(3)使用FutureTask對象作為Thread對象的target創建並啟動新線程。

(4)調用FutureTask對象的get()方法來獲得子線程執行結束后的返回值

實例代碼:

package test4;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class Main {

    public static void main(String[] args) {
        ThreadDemo td = new ThreadDemo();

        // 1.執行Callable方式,需要FutureTask實現類的支持,用於接收運算結果
        FutureTask<Integer> result = new FutureTask<>(td);
        new Thread(result).start();

        // 2.接收線程運算后的結果
        Integer sum;
        try {
            //等所有線程執行完,獲取值,因此FutureTask 可用於 閉鎖
            sum = result.get();
            System.out.println("-----------------------------");
            System.out.println(sum);
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
    }
} 
class ThreadDemo implements Callable<Integer> {

    @Override
    public Integer call() throws Exception {
        int sum = 0;
        for (int i = 0; i <= 10; i++) {
            System.out.println(i);
            sum += i;
        }
        return sum;
    }
}

結果

0
1
2
3
4
5
6
7
8
9
10
-----------------------------
55

 

 

四、創建線程的三種方式的對比

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM