日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Java多線程實現(xiàn)的三種方式

Java多線程實現(xiàn)方式主要有三種:繼承Thread類、實現(xiàn)Runnable接口、使用ExecutorService、Callable、Future實現(xiàn)有返回結果的多線程。其中前兩種方式線程執(zhí)行完后都沒有返回值,只有最后一種是帶返回值的。

寶興ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)建站的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書合作)期待與您的合作!

1、繼承Thread類實現(xiàn)多線程

繼承Thread類的方法盡管被我列為一種多線程實現(xiàn)方式,但Thread本質上也是實現(xiàn)了Runnable接口的一個實例,它代表一個線程的實例,并且,啟動線程的唯一方法就是通過Thread類的start()實例方法。start()方法是一個native方法,它將啟動一個新線程,并執(zhí)行run()方法。這種方式實現(xiàn)多線程很簡單,通過自己的類直接extend Thread,并復寫run()方法,就可以啟動新線程并執(zhí)行自己定義的run()方法。例如:

 
 
 
  1. public class MyThread extends Thread {   
  2.   public void run() {   
  3.    System.out.println("MyThread.run()");   
  4.   }   

在合適的地方啟動線程如下:

 
 
 
  1. MyThread myThread1 = new MyThread();  
  2. MyThread myThread2 = new MyThread();   
  3. myThread1.start();   
  4. myThread2.start(); 

2、實現(xiàn)Runnable接口方式實現(xiàn)多線程

如果自己的類已經(jīng)extends另一個類,就無法直接extends Thread,此時,必須實現(xiàn)一個Runnable接口,如下:

 
 
 
  1. public class MyThread extends OtherClass implements Runnable {   
  2.   public void run() {   
  3.    System.out.println("MyThread.run()");   
  4.   }   

為了啟動MyThread,需要首先實例化一個Thread,并傳入自己的MyThread實例:

 
 
 
  1. MyThread myThread = new MyThread();   
  2. Thread thread = new Thread(myThread); 
  3. thread.start(); 

事實上,當傳入一個Runnable target參數(shù)給Thread后,Thread的run()方法就會調用target.run(),參考JDK源代碼:

 
 
 
  1. public void run() {   
  2.   if (target != null) {   
  3.    target.run();   
  4.   }   

3、使用ExecutorService、Callable、Future實現(xiàn)有返回結果的多線程

ExecutorService、Callable、Future這個對象實際上都是屬于Executor框架中的功能類。想要詳細了解Executor框架的可以訪問http://www.javaeye.com/topic/366591 ,這里面對該框架做了很詳細的解釋。返回結果的線程是在JDK1.5中引入的新特征,確實很實用,有了這種特征我就不需要再為了得到返回值而大費周折了,而且即便實現(xiàn)了也可能漏洞百出。

可返回值的任務必須實現(xiàn)Callable接口,類似的,無返回值的任務必須Runnable接口。執(zhí)行Callable任務后,可以獲取一個Future的對象,在該對象上調用get就可以獲取到Callable任務返回的Object了,再結合線程池接口ExecutorService就可以實現(xiàn)傳說中有返回結果的多線程了。下面提供了一個完整的有返回結果的多線程測試例子,在JDK1.5下驗證過沒問題可以直接使用。代碼如下:

 
 
 
  1. import java.util.concurrent.*;   
  2. import java.util.Date;   
  3. import java.util.List;   
  4. import java.util.ArrayList;     
  5.  
  6. /** 
  7.  
  8. * 有返回值的線程 
  9.  
  10. */  
  11.  
  12. @SuppressWarnings("unchecked")   
  13. public class Test {   
  14. public static void main(String[] args) throws ExecutionException,   
  15.    InterruptedException {   
  16.   System.out.println("----程序開始運行----");   
  17.   Date date1 = new Date();     
  18.   int taskSize = 5;   
  19.   // 創(chuàng)建一個線程池   
  20.   ExecutorService pool = Executors.newFixedThreadPool(taskSize);   
  21.   // 創(chuàng)建多個有返回值的任務   
  22.   List list = new ArrayList();   
  23.   for (int i = 0; i < taskSize; i++) {  
  24.    Callable c = new MyCallable(i + " ");   
  25.    // 執(zhí)行任務并獲取Future對象   
  26.    Future f = pool.submit(c);   
  27.    // System.out.println(">>>" + f.get().toString());   
  28.    list.add(f);   
  29.   }   
  30.   // 關閉線程池   
  31.   pool.shutdown();    
  32.  
  33.   // 獲取所有并發(fā)任務的運行結果   
  34.   for (Future f : list) {   
  35.    // 從Future對象上獲取任務的返回值,并輸出到控制臺   
  36.    System.out.println(">>>" + f.get().toString());  
  37.   }     
  38.  
  39.   Date date2 = new Date();   
  40.   System.out.println("----程序結束運行----,程序運行時間【"   
  41.     + (date2.getTime() - date1.getTime()) + "毫秒】");   
  42. }   
  43. }     
  44.  
  45. class MyCallable implements Callable {   
  46. private String taskNum; 
  47.  
  48. MyCallable(String taskNum) {   
  49.   this.taskNum = taskNum;   
  50. }  
  51.  
  52. public Object call() throws Exception {   
  53.   System.out.println(">>>" + taskNum + "任務啟動");   
  54.   Date dateTmp1 = new Date();   
  55.   Thread.sleep(1000);   
  56.   Date dateTmp2 = new Date();   
  57.   long time = dateTmp2.getTime() - dateTmp1.getTime();   
  58.   System.out.println(">>>" + taskNum + "任務終止");   
  59.   return taskNum + "任務返回運行結果,當前任務時間【" + time + "毫秒】"; 
  60. }   
  61. 代碼說明:

    上述代碼中Executors類,提供了一系列工廠方法用于創(chuàng)先線程池,返回的線程池都實現(xiàn)了ExecutorService接口。

    public static ExecutorService newFixedThreadPool(int nThreads) 創(chuàng)建固定數(shù)目線程的線程池。

    public static ExecutorService newCachedThreadPool() 創(chuàng)建一個可緩存的線程池,調用execute 將重用以前構造的線程(如果線程可用)。如果現(xiàn)有線程沒有可用的,則創(chuàng)建一個新線程并添加到池中。終止并從緩存中移除那些已有 60 秒鐘未被使用的線程。

    public static ExecutorService newSingleThreadExecutor() 創(chuàng)建一個單線程化的Executor。

    public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) 創(chuàng)建一個支持定時及周期性的任務執(zhí)行的線程池,多數(shù)情況下可用來替代Timer類。

    ExecutoreService提供了submit()方法,傳遞一個Callable,或Runnable,返回Future。如果Executor后臺線程池還沒有完成Callable的計算,這調用返回Future對象的get()方法,會阻塞直到計算完成。


    本文題目:Java多線程實現(xiàn)的三種方式
    鏈接地址:http://www.5511xx.com/article/dpeigip.html