欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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)文章

  • 分布式開發(fā)醫(yī)療掛號系統(tǒng)數(shù)據(jù)字典模塊前后端實現(xiàn)

    分布式開發(fā)醫(yī)療掛號系統(tǒng)數(shù)據(jù)字典模塊前后端實現(xiàn)

    這篇文章主要為大家介紹了分布式開發(fā)醫(yī)療掛號系統(tǒng)數(shù)據(jù)字典模塊前后端實現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-04-04
  • Java中常見的XML解析方法與應(yīng)用詳解

    Java中常見的XML解析方法與應(yīng)用詳解

    XML(eXtensible Markup Language)是一種用于存儲和傳輸數(shù)據(jù)的標(biāo)記語言,被廣泛應(yīng)用于表示和交換獨立于應(yīng)用程序和硬件平臺的結(jié)構(gòu)化信息,下面我們就來看看它的常見解析方法有哪些吧
    2024-01-01
  • Java由淺入深學(xué)習(xí)數(shù)組的使用

    Java由淺入深學(xué)習(xí)數(shù)組的使用

    數(shù)組對于每一門編程語言來說都是重要的數(shù)據(jù)結(jié)構(gòu)之一,當(dāng)然不同語言對數(shù)組的實現(xiàn)及處理也不盡相同。Java?語言中提供的數(shù)組是用來存儲固定大小的同類型元素
    2022-05-05
  • Spring Boot集成Seata實現(xiàn)基于AT模式的分布式事務(wù)的解決方案

    Spring Boot集成Seata實現(xiàn)基于AT模式的分布式事務(wù)的解決方案

    Seata 是一款開源的分布式事務(wù)解決方案,致力于提供高性能和簡單易用的分布式事務(wù)服務(wù),這篇文章主要介紹了Spring Boot集成Seata實現(xiàn)基于AT模式的分布式事務(wù),需要的朋友可以參考下
    2024-08-08
  • Java實現(xiàn)一個小說采集程序的簡單實例

    Java實現(xiàn)一個小說采集程序的簡單實例

    下面小編就為大家?guī)硪黄狫ava實現(xiàn)一個小說采集程序的簡單實例。小編覺得挺不錯的, 現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-06-06
  • Java Swing中的JButton、JComboBox、JList和JColorChooser組件使用案例

    Java Swing中的JButton、JComboBox、JList和JColorChooser組件使用案例

    這篇文章主要介紹了Java Swing中的按鈕(JButton)、組合框(JComboBox)、下拉列表(JList)和顏色選擇器(JColorChooser)組件使用案例,需要的朋友可以參考下
    2014-10-10
  • Java設(shè)計模式之抽象工廠模式詳解

    Java設(shè)計模式之抽象工廠模式詳解

    這篇文章主要介紹了Java設(shè)計模式之抽象工廠模式詳解,抽象工廠是一種為訪問類提供一個創(chuàng)建一組相關(guān)或相互依賴對象的接口,且訪問類無須指定所要產(chǎn)品的具體類就能得到同族的、不同等級的產(chǎn)品的模式結(jié)構(gòu),需要的朋友可以參考下
    2023-09-09
  • pom文件中${project.basedir}的使用

    pom文件中${project.basedir}的使用

    這篇文章主要介紹了pom文件中${project.basedir}的使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Java中的鎖分類的詳細(xì)介紹

    Java中的鎖分類的詳細(xì)介紹

    這篇文章主要介紹了Java中的鎖分類的詳細(xì)介紹,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • MyBatis一次執(zhí)行多條SQL語句的操作

    MyBatis一次執(zhí)行多條SQL語句的操作

    這篇文章主要介紹了MyBatis一次執(zhí)行多條SQL語句的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12

最新評論