服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - Java教程 - Spring Boot对Future模式的支持详解

Spring Boot对Future模式的支持详解

2021-07-01 14:57Zejun-Liu Java教程

这篇文章主要给大家介绍了关于Spring Boot对Future模式的支持的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用spring boot具有一定的参考学习价值,需要的朋友们下面来一起看看吧

前言

我们在实际项目中有些复杂运算、耗时操作,就可以利用多线程来充分利用cpu,提高系统吞吐量。springboot对多线程支持非常好,对我们的开发非常便捷。

future模式是多线程开发中非常常见的一种设计模式。核心思想是异步调用。当我们执行一个方法时,方法中有多个耗时任务需要同时去做,而且又不着急等待这个结果时可以让客户端立即返回然后,后台慢慢去计算任务。

当我们做一件事的时候需要等待,那么我们就可以在这个等待时间内来去做其它事情,这样就可以充分利用时间。比如我们点外卖,需要一段时间,那么我们在等外卖的时间里可以看点书,看个电影。这就是典型的future模式。如果是普通模式的话,就是等外卖的时候就等外卖,外卖到了后再去看书,极大的浪费时间。

springboot对future模式支持非常好,只需要简单的代码就能实现。

1.future的相关方法

  • boolean cancel(boolean mayinterruptifrunning);//可以在任务执行过程中取消任务
  • boolean iscancelled();//判断future任务是否取消
  • boolean isdone();//判断任务是否完成
  • v get();//获取任务最终结果,这是一个阻塞方法,会等待任务执行好才会执行后面的代码
  • v get(long timeout, timeunit unit);//有等待时常的get方法,等待时间到了后仍然没有计算完成,则抛异常

2.需要的注解

springboot 配置多线程需要两个注解

1、@enableasync

在配置类中通过加@enableasync开启对异步任务的支持
2、@async

在需要执行的方法上加@async表明该方法是个异步方法,如果加在类级别上,则表明类所有的方法都是异步方法

3.配置代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@configuration
@enableasync
public class asyncconfig implements asyncconfigurer {
 
 @override
 public executor getasyncexecutor() {
 threadpooltaskexecutor taskexecutor = new threadpooltaskexecutor();
 //核心线程数
 taskexecutor.setcorepoolsize(8);
 //最大线程数
 taskexecutor.setmaxpoolsize(16);
 //队列大小
 taskexecutor.setqueuecapacity(100);
 taskexecutor.initialize();
 return taskexecutor;
 }
}

4.futureservice

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@service
public class futureservice {
 
 @async
 public future<string> futuretest() throws interruptedexception {
 system.out.println("任务执行开始,需要:1000ms");
 for (int i = 0; i < 10; i++) {
  thread.sleep(100);
  system.out.println("do:" + i);
 }
 system.out.println("完成任务");
 return new asyncresult<>(thread.currentthread().getname());
 }
}

【注】这里的方法自动被注入使用上文配置的threadpooltaskexecutor

5.测试代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@resource
private futureservice futureservice;
 
@test
public void futuretest() throws interruptedexception, executionexception {
 long start = system.currenttimemillis();
 system.out.println("开始");
 //耗时任务
 future<string> future = futureservice.futuretest();
 //另外一个耗时任务
 thread.sleep(500);
 system.out.println("另外一个耗时任务,需要500ms");
 
 string s = future.get();
 system.out.println("计算结果输出:" + s);
 system.out.println("共耗时:" + (system.currenttimemillis() - start));
}

6.运行结果

开始
2019-01-07 23:50:34.726 info 14648 --- [ main] o.s.s.concurrent.threadpooltaskexecutor : initializing executorservice
任务执行开始,需要:1000ms
do:0
do:1
do:2
do:3
另外一个耗时任务,需要500ms
do:4
do:5
do:6
do:7
do:8
do:9
完成任务
计算结果输出:threadpooltaskexecutor-1
共耗时:1016

process finished with exit code 0

本来需要至少1500ms 执行的任务现在只需要1016ms,因为在执行耗时任务1的同时也在执行耗时任务2,两个任务并行执行,这就是future模式的好处,在等待时间内去执行其它任务,能够充分利用时间

【注】本文基于springboot 2.0

github 连接

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。

原文链接:https://juejin.im/post/5c344dba6fb9a049ab0dc9b7

延伸 · 阅读

精彩推荐