Java并发Futures和Callables类实例详解

 更新时间:2024年05月08日 09:52:42   作者:智慧浩海  
Callable对象返回Future对象,该对象提供监视线程执行的任务进度的方法, Future对象可用于检查Callable的状态,然后线程完成后从Callable中检索结果,这篇文章给大家介绍Java并发Futures和Callables类的相关知识,感兴趣的朋友一起看看吧

java.util.concurrent.Callable对象可以返回由线程完成的计算结果,而runnable接口只能运行线程。 Callable对象返回Future对象,该对象提供监视线程执行的任务进度的方法。 Future对象可用于检查Callable的状态,然后线程完成后从Callable中检索结果。 它还提供超时功能。

语法

//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程序显示了基于线程的环境中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;    
      }
   }
}

这将产生以下结果。

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

到此这篇关于Java并发Futures和Callables类的文章就介绍到这了,更多相关Java Futures和Callables类内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java如何调用Matlab程序

    Java如何调用Matlab程序

    这篇文章主要介绍了Java如何调用Matlab程序的操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • SpringMVC中的@ControllerAdvice使用场景详解

    SpringMVC中的@ControllerAdvice使用场景详解

    这篇文章主要介绍了SpringMVC中的@ControllerAdvice使用场景详解,在Spring MVC进行调用的过程中,会有很多的特殊的需求,比如全局异常,分页信息和分页搜索条件,请求时带来返回时还得回显页面,需要的朋友可以参考下
    2024-01-01
  • Spring Cloud Alibaba和Dubbo融合实现

    Spring Cloud Alibaba和Dubbo融合实现

    这篇文章主要介绍了Spring Cloud Alibaba和Dubbo融合实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-04-04
  • Java之PreparedStatement的使用详解

    Java之PreparedStatement的使用详解

    这篇文章主要介绍了Java之PreparedStatement的使用详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • java如何根据PostMan发送请求设置接口请求工具类

    java如何根据PostMan发送请求设置接口请求工具类

    在Java中调用第三方接口可以通过不同的方式,如使用GET、POST等请求,关键点包括设置正确的请求方式、URL、参数(params)、头信息(headers)和请求体(body),对于不同的数据格式,如XML和JSON,需在header中声明内容类型
    2024-09-09
  • Java中Properties的使用详解

    Java中Properties的使用详解

    这篇文章主要介绍了Java中Properties的使用详解的相关资料,需要的朋友可以参考下
    2016-05-05
  • java异步编程的7种实现方式小结

    java异步编程的7种实现方式小结

    异步处理的实现方式有很多种,常见多线程,消息中间件,发布订阅的广播模式,本文就详细的介绍java异步编程的7种实现方式,感兴趣的可以了解一下
    2023-03-03
  • IDEA实现添加 前进后退 到工具栏的操作

    IDEA实现添加 前进后退 到工具栏的操作

    这篇文章主要介绍了IDEA 前进 后退 添加到工具栏的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-02-02
  • Spring注解开发@Bean和@ComponentScan使用案例

    Spring注解开发@Bean和@ComponentScan使用案例

    这篇文章主要介绍了Spring注解开发@Bean和@ComponentScan使用案例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-09-09
  • Java easyui树形表格TreeGrid的实现代码

    Java easyui树形表格TreeGrid的实现代码

    这篇文章主要为大家详细介绍了Java easyui树形表格TreeGrid的实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-03-03

最新评论