最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java并發(fā)Futures和Callables類實例詳解

 更新時間:2024年05月08日 09:52:42   作者:智慧浩海  
Callable對象返回Future對象,該對象提供監(jiān)視線程執(zhí)行的任務(wù)進度的方法, Future對象可用于檢查Callable的狀態(tài),然后線程完成后從Callable中檢索結(jié)果,這篇文章給大家介紹Java并發(fā)Futures和Callables類的相關(guān)知識,感興趣的朋友一起看看吧

java.util.concurrent.Callable對象可以返回由線程完成的計算結(jié)果,而runnable接口只能運行線程。 Callable對象返回Future對象,該對象提供監(jiān)視線程執(zhí)行的任務(wù)進度的方法。 Future對象可用于檢查Callable的狀態(tài),然后線程完成后從Callable中檢索結(jié)果。 它還提供超時功能。

語法

//submit the callable using ThreadExecutor
//and get the result as a Future object
Future result10 = executor.submit(new FactorialService(10));
//get the result using get method of the Future object
//get method waits till the thread execution and then return the result of the execution. 
Long factorial10 = result10.get();

實例

以下TestThread程序顯示了基于線程的環(huán)境中FuturesCallables的使用。

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class TestThread {
   public static void main(final String[] arguments) throws InterruptedException, ExecutionException {
      ExecutorService executor = Executors.newSingleThreadExecutor();
      System.out.println("Factorial Service called for 10!");
      Future<Long> result10 = executor.submit(new FactorialService(10));
      System.out.println("Factorial Service called for 20!");
      Future<Long> result20 = executor.submit(new FactorialService(20));
      Long factorial10 = result10.get();
      System.out.println("10! = " + factorial10);
      Long factorial20 = result20.get();
      System.out.println("20! = " + factorial20);
      executor.shutdown();
   }  
   static class FactorialService implements Callable<Long>{
      private int number;
      public FactorialService(int number) {
         this.number = number;
      }
      @Override
      public Long call() throws Exception {
         return factorial();
      }
      private Long factorial() throws InterruptedException{
         long result = 1; 
         while (number != 0) { 
            result = number * result; 
            number--; 
            Thread.sleep(100); 
         } 
         return result;    
      }
   }
}

這將產(chǎn)生以下結(jié)果。

Factorial Service called for 10!
Factorial Service called for 20!
10! = 3628800
20! = 2432902008176640000

到此這篇關(guān)于Java并發(fā)Futures和Callables類的文章就介紹到這了,更多相關(guān)Java Futures和Callables類內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java如何調(diào)用Matlab程序

    Java如何調(diào)用Matlab程序

    這篇文章主要介紹了Java如何調(diào)用Matlab程序的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • SpringMVC中的@ControllerAdvice使用場景詳解

    SpringMVC中的@ControllerAdvice使用場景詳解

    這篇文章主要介紹了SpringMVC中的@ControllerAdvice使用場景詳解,在Spring?MVC進行調(diào)用的過程中,會有很多的特殊的需求,比如全局異常,分頁信息和分頁搜索條件,請求時帶來返回時還得回顯頁面,需要的朋友可以參考下
    2024-01-01
  • Spring Cloud Alibaba和Dubbo融合實現(xiàn)

    Spring Cloud Alibaba和Dubbo融合實現(xiàn)

    這篇文章主要介紹了Spring Cloud Alibaba和Dubbo融合實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-04-04
  • Java之PreparedStatement的使用詳解

    Java之PreparedStatement的使用詳解

    這篇文章主要介紹了Java之PreparedStatement的使用詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • java如何根據(jù)PostMan發(fā)送請求設(shè)置接口請求工具類

    java如何根據(jù)PostMan發(fā)送請求設(shè)置接口請求工具類

    在Java中調(diào)用第三方接口可以通過不同的方式,如使用GET、POST等請求,關(guān)鍵點包括設(shè)置正確的請求方式、URL、參數(shù)(params)、頭信息(headers)和請求體(body),對于不同的數(shù)據(jù)格式,如XML和JSON,需在header中聲明內(nèi)容類型
    2024-09-09
  • Java中Properties的使用詳解

    Java中Properties的使用詳解

    這篇文章主要介紹了Java中Properties的使用詳解的相關(guān)資料,需要的朋友可以參考下
    2016-05-05
  • java異步編程的7種實現(xiàn)方式小結(jié)

    java異步編程的7種實現(xiàn)方式小結(jié)

    異步處理的實現(xiàn)方式有很多種,常見多線程,消息中間件,發(fā)布訂閱的廣播模式,本文就詳細的介紹java異步編程的7種實現(xiàn)方式,感興趣的可以了解一下
    2023-03-03
  • IDEA實現(xiàn)添加 前進后退 到工具欄的操作

    IDEA實現(xiàn)添加 前進后退 到工具欄的操作

    這篇文章主要介紹了IDEA 前進 后退 添加到工具欄的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • Spring注解開發(fā)@Bean和@ComponentScan使用案例

    Spring注解開發(fā)@Bean和@ComponentScan使用案例

    這篇文章主要介紹了Spring注解開發(fā)@Bean和@ComponentScan使用案例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-09-09
  • Java easyui樹形表格TreeGrid的實現(xiàn)代碼

    Java easyui樹形表格TreeGrid的實現(xiàn)代碼

    這篇文章主要為大家詳細介紹了Java easyui樹形表格TreeGrid的實現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03

最新評論

汤阴县| 横山县| 正定县| 萝北县| 安顺市| 禹州市| 荔波县| 望都县| 景德镇市| 措勤县| 宁阳县| 缙云县| 新建县| 团风县| 商都县| 鄂伦春自治旗| 镇江市| 横峰县| 社旗县| 松潘县| 南川市| 资兴市| 宁化县| 鄂托克旗| 汾西县| 伽师县| 威信县| 新田县| 乌拉特前旗| 勐海县| 峡江县| 长治市| 荔波县| 平和县| 台东市| 开平市| 沙洋县| 南丹县| 武冈市| 西华县| 泰宁县|