亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java利用future及時獲取多線程運(yùn)行結(jié)果

 更新時間:2017年10月11日 14:44:59   作者:clare-chen  
在Java編程中,有時候會需要及時獲取線程的運(yùn)行結(jié)果,本文就通過一個相關(guān)實(shí)例向大家介紹Java利用future及時獲取線程運(yùn)行結(jié)果的方法,需要的朋友可以參考。

Future接口是Java標(biāo)準(zhǔn)API的一部分,在java.util.concurrent包中。Future接口是Java線程Future模式的實(shí)現(xiàn),可以來進(jìn)行異步計(jì)算。

有了Future就可以進(jìn)行三段式的編程了,1.啟動多線程任務(wù)2.處理其他事3.收集多線程任務(wù)結(jié)果。從而實(shí)現(xiàn)了非阻塞的任務(wù)調(diào)用。在途中遇到一個問題,那就是雖然能異步獲取結(jié)果,但是Future的結(jié)果需要通過isdone來判斷是否有結(jié)果,或者使用get()函數(shù)來阻塞式獲取執(zhí)行結(jié)果。這樣就不能實(shí)時跟蹤其他線程的結(jié)果狀態(tài)了,所以直接使用get還是要慎用,最好配合isdone來使用。

這里有一種更好的方式來實(shí)現(xiàn)對任意一個線程運(yùn)行完成后的結(jié)果都能及時獲取的辦法:使用CompletionService,它內(nèi)部添加了阻塞隊(duì)列,從而獲取future中的值,然后根據(jù)返回值做對應(yīng)的處理。一般future使用和CompletionService使用的兩個測試案例如下:

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
/**
 * 多線程執(zhí)行,異步獲取結(jié)果
 * 
 * @author i-clarechen
 *
 */
public class AsyncThread {
  public static void main(String[] args) {
    AsyncThread t = new AsyncThread();
    List<Future<String>> futureList = new ArrayList<Future<String>>();
    t.generate(3, futureList);
    t.doOtherThings();
    t.getResult(futureList);
  }
  /**
   * 生成指定數(shù)量的線程,都放入future數(shù)組
   * 
   * @param threadNum
   * @param fList
   */
  public void generate(int threadNum, List<Future<String>> fList) {
    ExecutorService service = Executors.newFixedThreadPool(threadNum);
    for (int i = 0; i < threadNum; i++) {
      Future<String> f = service.submit(getJob(i));
      fList.add(f);
    }
    service.shutdown();
  }
  /**
   * other things
   */
  public void doOtherThings() {
    try {
      for (int i = 0; i < 3; i++) {
        System.out.println("do thing no:" + i);
        Thread.sleep(1000 * (new Random().nextInt(10)));
      }
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
  /**
   * 從future中獲取線程結(jié)果,打印結(jié)果
   * 
   * @param fList
   */
  public void getResult(List<Future<String>> fList) {
    ExecutorService service = Executors.newSingleThreadExecutor();
    service.execute(getCollectJob(fList));
    service.shutdown();
  }
  /**
   * 生成指定序號的線程對象
   * 
   * @param i
   * @return
   */
  public Callable<String> getJob(final int i) {
    final int time = new Random().nextInt(10);
    return new Callable<String>() {
      @Override
      public String call() throws Exception {
        Thread.sleep(1000 * time);
        return "thread-" + i;
      }
    };
  }
  /**
   * 生成結(jié)果收集線程對象
   * 
   * @param fList
   * @return
   */
  public Runnable getCollectJob(final List<Future<String>> fList) {
    return new Runnable() {
      public void run() {
        for (Future<String> future : fList) {
          try {
            while (true) {
              if (future.isDone() && !future.isCancelled()) {
                System.out.println("Future:" + future
                    + ",Result:" + future.get());
                break;
              } else {
                Thread.sleep(1000);
              }
            }
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
      }
    };
  }
}

運(yùn)行結(jié)果打印和future放入列表時的順序一致,為0,1,2:

do thing no:0
do thing no:1
do thing no:2
Future:java.util.concurrent.FutureTask@68e1ca74,Result:thread-0
Future:java.util.concurrent.FutureTask@3fb2bb77,Result:thread-1
Future:java.util.concurrent.FutureTask@6f31a24c,Result:thread-2

下面是先執(zhí)行完的線程先處理的方案:

import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingDeque;
public class testCallable {
  public static void main(String[] args) {
    try {
      completionServiceCount();
    } catch (InterruptedException e) {
      e.printStackTrace();
    } catch (ExecutionException e) {
      e.printStackTrace();
    }
  }
  /**
   * 使用completionService收集callable結(jié)果
   * @throws ExecutionException 
   * @throws InterruptedException 
   */
  public static void completionServiceCount() throws InterruptedException, ExecutionException {
    ExecutorService executorService = Executors.newCachedThreadPool();
    CompletionService<Integer> completionService = new ExecutorCompletionService<Integer>(
        executorService);
    int threadNum = 5;
    for (int i = 0; i < threadNum; i++) {
      completionService.submit(getTask(i));
    }
    int sum = 0;
    int temp = 0;
    for(int i=0;i<threadNum;i++){
      temp = completionService.take().get();
      sum += temp;
      System.out.print(temp + "\t");
    }
    System.out.println("CompletionService all is : " + sum);
    executorService.shutdown();
  }
  public static Callable<Integer> getTask(final int no) {
    final Random rand = new Random();
    Callable<Integer> task = new Callable<Integer>() {
      @Override
      public Integer call() throws Exception {
        int time = rand.nextInt(100)*100;
        System.out.println("thead:"+no+" time is:"+time);
        Thread.sleep(time);
        return no;
      }
    };
    return task;
  }
}

運(yùn)行結(jié)果為最先結(jié)束的線程結(jié)果先被處理:

thead:0 time is:4200
thead:1 time is:6900
thead:2 time is:2900
thead:3 time is:9000
thead:4 time is:7100
  0  1  4  3  CompletionService all is : 10

總結(jié)

以上就是本文關(guān)于Java利用future及時獲取線程運(yùn)行結(jié)果的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以參閱:Java多線程ForkJoinPool實(shí)例詳解、Java通過賣票理解多線程、Java多線程之readwritelock讀寫分離的實(shí)現(xiàn)代碼等,有什么問題可以隨時留言,歡迎大家交流討論。

相關(guān)文章

  • SpringBoot集成ElaticJob定時器的實(shí)現(xiàn)代碼

    SpringBoot集成ElaticJob定時器的實(shí)現(xiàn)代碼

    這篇文章主要介紹了SpringBoot集成ElaticJob定時器的實(shí)現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-06-06
  • dom4j讀取XML文件詳解

    dom4j讀取XML文件詳解

    這篇文章主要為大家詳細(xì)介紹了dom4j讀取XML文件的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • mybatis Example的Criteria用法:or與isNull詳解

    mybatis Example的Criteria用法:or與isNull詳解

    這篇文章主要介紹了mybatis Example的Criteria用法:or與isNull詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • 解決java maven項(xiàng)目找不到j(luò)console-1.8.0.jar和tools-1.8.0.jar包問題

    解決java maven項(xiàng)目找不到j(luò)console-1.8.0.jar和tools-1.8.0.jar包問題

    這篇文章主要介紹了解決java maven項(xiàng)目找不到j(luò)console-1.8.0.jar和tools-1.8.0.jar包問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • 手工搭建Servlet實(shí)現(xiàn)

    手工搭建Servlet實(shí)現(xiàn)

    現(xiàn)在作為一個Java程序員,我們已經(jīng)習(xí)慣了使用IDE和Web框架進(jìn)行開發(fā),IDE幫助我們做了編譯、打包的工作。Spring框架則幫助我們實(shí)現(xiàn)了Servlet接口,并把Servlet容器注冊到了Web容器中。本文主要介紹了Servlet手工搭建,感興趣的可以了解一下
    2021-07-07
  • SpringBoot如何統(tǒng)一清理數(shù)據(jù)

    SpringBoot如何統(tǒng)一清理數(shù)據(jù)

    這篇文章主要介紹了SpringBoot如何統(tǒng)一清理數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • Java easyexcel導(dǎo)出報(bào)內(nèi)存溢出的問題解決

    Java easyexcel導(dǎo)出報(bào)內(nèi)存溢出的問題解決

    在Java開發(fā)時,使用EasyExcel處理大數(shù)據(jù)量導(dǎo)出可能遇到內(nèi)存溢出問題,本文深入分析了內(nèi)存溢出的原因,并提出了優(yōu)化策略,感興趣的可以了解一下
    2024-10-10
  • MyBatis快速入門

    MyBatis快速入門

    MyBatis是支持普通SQL查詢,存儲過程和高級映射的優(yōu)秀持久層框架。MyBatis消除了幾乎所有的JDBC代碼和參數(shù)的手工設(shè)置以及結(jié)果集的檢索。想要學(xué)好它,那就要從MyBatis基礎(chǔ)知識學(xué)起,下面跟著小編一起來看下吧
    2017-03-03
  • JAVA動態(tài)代理模式(從現(xiàn)實(shí)生活角度理解代碼原理)

    JAVA動態(tài)代理模式(從現(xiàn)實(shí)生活角度理解代碼原理)

    本文主要介紹了JAVA動態(tài)代理模式(從現(xiàn)實(shí)生活角度理解代碼原理)的相關(guān)知識。具有很好的參考價值。下面跟著小編一起來看下吧
    2017-03-03
  • Java的Spring框架中AOP項(xiàng)目的一般配置和部署教程

    Java的Spring框架中AOP項(xiàng)目的一般配置和部署教程

    這篇文章主要介紹了Java的Spring框架中AOP項(xiàng)目的一般配置和部署教程,AOP面向方面編程的項(xiàng)目部署結(jié)構(gòu)都比較類似,因而也被看作是Spring的一種設(shè)計(jì)模式使用,需要的朋友可以參考下
    2016-04-04

最新評論