Java多線程實現的四種方式


Java多線程實現的方式有四種

    1.繼承Thread類,重寫run方法
    2.實現Runnable接口,重寫run方法,實現Runnable接口的實現類的實例對象作為Thread構造函數的target
    3.通過Callable和FutureTask創建線程
    4.通過線程池創建線程

    前面兩種可以歸結為一類:無返回值,原因很簡單,通過重寫run方法,run方式的返回值是void,所以沒有辦法返回結果。
    后面兩種可以歸結成一類:有返回值,通過Callable接口,就要實現call方法,這個方法的返回值是Object,所以返回的結果可以放在Object對象中。

第一種:繼承Thread類,重寫該類的run()方法。

 

 1 class MyThread extends Thread {  2  3 private int i = 0;  4  5  @Override  6 public void run() {  7 for (i = 0; i < 100; i++) {  8 System.out.println(Thread.currentThread().getName() + " " + i);  9  } 10  } 11 }

 

 1 public class ThreadTest {  2  3 public static void main(String[] args) {  4 for (int i = 0; i < 100; i++) {  5 System.out.println(Thread.currentThread().getName() + " " + i);  6 if (i == 30) {  7 Thread myThread1 = new MyThread(); // 創建一個新的線程 myThread1 此線程進入新建狀態  8 Thread myThread2 = new MyThread(); // 創建一個新的線程 myThread2 此線程進入新建狀態  9 myThread1.start(); // 調用start()方法使得線程進入就緒狀態 10 myThread2.start(); // 調用start()方法使得線程進入就緒狀態 11  } 12  } 13  } 14 }

如上所示,繼承Thread類,通過重寫run()方法定義了一個新的線程類MyThread,其中run()方法的方法體代表了線程需要完成的任務,稱之為線程執行體。當創建此線程類對象時一個新的線程得以創建,並進入到線程新建狀態。通過調用線程對象引用的start()方法,使得該線程進入到就緒狀態,此時此線程並不一定會馬上得以執行,這取決於CPU調度時機。

第二種:實現Runnable接口,並重寫該接口的run()方法。

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

 

 1 class MyRunnable implements Runnable {  2 private int i = 0;  3  4  @Override  5 public void run() {  6 for (i = 0; i < 100; i++) {  7 System.out.println(Thread.currentThread().getName() + " " + i);  8  }  9  } 10 }

 

 1 public class ThreadTest {  2  3 public static void main(String[] args) {  4 for (int i = 0; i < 100; i++) {  5 System.out.println(Thread.currentThread().getName() + " " + i);  6 if (i == 30) {  7 Runnable myRunnable = new MyRunnable(); // 創建一個Runnable實現類的對象  8 Thread thread1 = new Thread(myRunnable); // 將myRunnable作為Thread target創建新的線程  9 Thread thread2 = new Thread(myRunnable); 10 thread1.start(); // 調用start()方法使得線程進入就緒狀態 11  thread2.start(); 12  } 13  } 14  } 15 }

 

第三種:使用Callable和Future接口創建線程。

  a:創建Callable接口的實現類 ,並實現Call方法
  b:創建Callable實現類的實現,使用FutureTask類包裝Callable對象,該FutureTask對象封裝了Callable對象的Call方法的返回值
  c:使用FutureTask對象作為Thread對象的target創建並啟動線程
  d:調用FutureTask對象的get()來獲取子線程執行結束的返回值

 

 1 public class ThreadTest {  2  3 public static void main(String[] args) {  4  5 Callable<Integer> myCallable = new MyCallable(); // 創建MyCallable對象  6 FutureTask<Integer> ft = new FutureTask<Integer>(myCallable); //使用FutureTask來包裝MyCallable對象  7  8 for (int i = 0; i < 100; i++) {  9 System.out.println(Thread.currentThread().getName() + " " + i); 10 if (i == 30) { 11 Thread thread = new Thread(ft); //FutureTask對象作為Thread對象的target創建新的線程 12 thread.start(); //線程進入到就緒狀態 13  } 14  } 15 16 System.out.println("主線程for循環執行完畢.."); 17 18 try { 19 int sum = ft.get(); //取得新創建的新線程中的call()方法返回的結果 20 System.out.println("sum = " + sum); 21 } catch (InterruptedException e) { 22  e.printStackTrace(); 23 } catch (ExecutionException e) { 24  e.printStackTrace(); 25  } 26 27  } 28 } 29 30 31 class MyCallable implements Callable<Integer> { 32 private int i = 0; 33 34 // 與run()方法不同的是,call()方法具有返回值 35  @Override 36 public Integer call() { 37 int sum = 0; 38 for (; i < 100; i++) { 39 System.out.println(Thread.currentThread().getName() + " " + i); 40 sum += i; 41  } 42 return sum; 43  } 44 45 }

 

首先,我們發現,在實現Callable接口中,此時不再是run()方法了,而是call()方法,此call()方法作為線程執行體,同時還具有返回值!在創建新的線程時,是通過FutureTask來包裝MyCallable對象,同時作為了Thread對象的target。

第四種:通過線程池創建線程。

 1 public class ThreadDemo05{  2 private static int POOL_NUM = 10; //線程池數量  3 /**  4  * @param args  5  * @throws InterruptedException  6 */  7 public static void main(String[] args) throws InterruptedException {  8 // TODO Auto-generated method stub  9 ExecutorService executorService = Executors.newFixedThreadPool(5); 10 for(int i = 0; i<POOL_NUM; i++) { 11 RunnableThread thread = new RunnableThread(); 12 //Thread.sleep(1000); 13  executorService.execute(thread); 14  } 15 //關閉線程池 16  executorService.shutdown(); 17  } 18 } 19 20 class RunnableThread implements Runnable { 21  @Override 22 public void run() { 23 System.out.println("通過線程池方式創建的線程:" + Thread.currentThread().getName() + " "); 24  } 25 } 

ExecutorService、Callable都是屬於Executor框架。返回結果的線程是在JDK1.5中引入的新特征,還有Future接口也是屬於這個框架,有了這種特征得到返回值就很方便了。
通過分析可以知道,他同樣也是實現了Callable接口,實現了Call方法,所以有返回值。這也就是正好符合了前面所說的兩種分類

執行Callable任務后,可以獲取一個Future的對象,在該對象上調用get就可以獲取到Callable任務返回的Object了。get方法是阻塞的,即:線程無返回結果,get方法會一直等待。


再介紹Executors類:提供了一系列工廠方法用於創建線程池,返回的線程池都實現了ExecutorService接口。

    public static ExecutorService newFixedThreadPool(int nThreads)
    創建固定數目線程的線程池。
    public static ExecutorService newCachedThreadPool()
    創建一個可緩存的線程池,調用execute 將重用以前構造的線程(如果線程可用)。如果現有線程沒有可用的,則創建一個新線程並添加到池中。終止並從緩存中移除那些已有 60 秒鍾未被使用的線程。
    public static ExecutorService newSingleThreadExecutor()
    創建一個單線程化的Executor。
    public static ScheduledExecutorService newScheduledThreadPool(int
    corePoolSize)
    創建一個支持定時及周期性的任務執行的線程池,多數情況下可用來替代Timer類。
    ExecutoreService提供了submit()方法,傳遞一個Callable,或Runnable,返回Future。如果Executor后台線程池還沒有完成Callable的計算,這調用返回Future對象的get()方法,會阻塞直到計算完成。

 參考:https://www.cnblogs.com/lwbqqyumidi/p/3804883.html

  https://blog.csdn.net/u011480603/article/details/75332435/

 


免責聲明!

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



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