当前位置: 首页 > news >正文

Spring中@Async注解实现“方法”的异步调用

简单介绍
Spring为任务调度与异步方法执行提供了注解支持。通过在方法上设置@Async注解,可使得方法被异步调用。也就是说调用者会在调用时立即返回,而被调用方法的实际执行是交给Spring的TaskExecutor来完成。

 

同时加入扫描注解。

为了比较,先来一个同步调用

@Component
public class TestAsyncBean {
public void sayHello4() throws InterruptedException {
Thread.sleep(2 * 1000);//网络连接中 。。。消息发送中。。。
System.out.println("我爱你啊!");
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:/applicationContext.xml"})
public class TestAsync {
@Test
public void test_sayHello4() throws InterruptedException, ExecutionException {
System.out.println("你不爱我了么?");
testAsyncBean.sayHello4();
System.out.println("回的这么慢, 你肯定不爱我了, 我们还是分手吧。。。");
Thread.sleep(3 * 1000);// 不让主进程过早结束
}
}

 

输出结果:

你不爱我了么?
我爱你啊!
回的这么慢, 你肯定不爱我了, 我们还是分手吧。。。

同步调用会按代码顺序依次进行下去,如果哪里需要等待,那么就阻塞在那里,不再向下继续进行。

使用@Async的异步调用:

@Component
public class TestAsyncBean {
@Async
public void sayHello3() throws InterruptedException {
Thread.sleep(2 * 1000);//网络连接中 。。。消息发送中。。。
System.out.println("我爱你啊!");
}
}

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:/applicationContext.xml"})
public class TestAsync {
@Autowired
private TestAsyncBean testAsyncBean;
@Test
public void test_sayHello3() throws InterruptedException, ExecutionException {
System.out.println("你不爱我了么?");
testAsyncBean.sayHello3();
System.out.println("你竟无话可说, 我们分手吧。。。");
Thread.sleep(3 * 1000);// 不让主进程过早结束
}
}

  

输出结果:

你不爱我了么?
你竟无话可说, 我们分手吧。。。
我爱你啊!

异步调用,通过开启新的线程来执行调用的方法,不影响主线程。异步方法实际的执行交给了Spring的TaskExecutor来完成。

上面这种方式是没有返回值的,下面尝试有返回值的异步调用:

@Component
public class TestAsyncBean {
@Async
public String sayHello2() throws InterruptedException {
Thread.sleep(2 * 1000);//网络连接中 。。。消息发送中。。。
return "我爱你啊!";// 调用方调用后会立即返回,所以返回null
}
}

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:/applicationContext.xml"})
public class TestAsync {
@Autowired
private TestAsyncBean testAsyncBean;
@Test
public void test_sayHello2() throws InterruptedException, ExecutionException {
System.out.println("你不爱我了么?");
System.out.println(testAsyncBean.sayHello2());
System.out.println("你说的啥? 我们还是分手吧。。。");
Thread.sleep(3 * 1000);// 不让主进程过早结束
}
}

输出结果

输出结你不爱我了么?
null
你说的啥? 我们还是分手吧。。。

接获取返回值得方式是不行的,这里就需要用到异步回调,异步方法返回值必须为Future<>,就像Callable与Future。

下面通过AsyncResult<>来获得异步调用的返回值:

@Component
public class TestAsyncBean {
@Async
public Future<String> sayHello1() throws InterruptedException {
int thinking = 2;
Thread.sleep(thinking * 1000);//网络连接中 。。。消息发送中。。。
System.out.println("我爱你啊!");
return new AsyncResult<String>("发送消息用了"+thinking+"秒");
}
}

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:/applicationContext.xml"})
public class TestAsync {
@Autowired
private TestAsyncBean testAsyncBean;
@Test
public void test_sayHello1() throws InterruptedException, ExecutionException {
Future<String> future = null;
System.out.println("你不爱我了么?");
future = testAsyncBean.sayHello1();
System.out.println("你竟无话可说, 我们分手吧。。。");
Thread.sleep(3 * 1000);// 不让主进程过早结束
System.out.println(future.get());
}
}

输出结果:

你不爱我了么?
你竟无话可说, 我们分手吧。。。
我爱你啊!
发送消息用了2秒

转载于:https://www.cnblogs.com/baizhanshi/p/10141838.html

相关文章:

  • Sniffer 使用详解
  • mac定时任务
  • Java开发,表单提交中发生中文乱码的问题。
  • 吐血推荐——(一级集成资质投标文件)绝密!!
  • 删除链表的倒数第N个节点
  • 计算机网络安全《社会工程学》——欺骗的艺术
  • Docker 容器的运行(八)
  • 刘翔那点事
  • 大家一起学CCNP BSCI路由
  • 数据结构开发(18):归并排序和快速排序
  • 工程文档管理
  • http和Https简介、详解
  • System.IO.StreamWriter写UTF-8文件取消写入BOM
  • asp.net core MVC 控制器,接收参数,数据绑定
  • Web站点风格切换的实现
  • 8年软件测试工程师感悟——写给还在迷茫中的朋友
  • Android 初级面试者拾遗(前台界面篇)之 Activity 和 Fragment
  • es的写入过程
  • Github访问慢解决办法
  • HTML-表单
  • iOS编译提示和导航提示
  • Javascript Math对象和Date对象常用方法详解
  • Java程序员幽默爆笑锦集
  • Java知识点总结(JDBC-连接步骤及CRUD)
  • js
  • js如何打印object对象
  • maven工程打包jar以及java jar命令的classpath使用
  • Mybatis初体验
  • Python学习之路13-记分
  • React+TypeScript入门
  • Service Worker
  • Spring技术内幕笔记(2):Spring MVC 与 Web
  • SpriteKit 技巧之添加背景图片
  • Vim 折腾记
  • Vue2.0 实现互斥
  • 产品三维模型在线预览
  • 简单实现一个textarea自适应高度
  • 看域名解析域名安全对SEO的影响
  • 聊聊spring cloud的LoadBalancerAutoConfiguration
  • 区块链分支循环
  • 融云开发漫谈:你是否了解Go语言并发编程的第一要义?
  • 使用 Xcode 的 Target 区分开发和生产环境
  • 数组的操作
  • LIGO、Virgo第三轮探测告捷,同时探测到一对黑洞合并产生的引力波事件 ...
  • ​创新驱动,边缘计算领袖:亚马逊云科技海外服务器服务再进化
  • (1)Map集合 (2)异常机制 (3)File类 (4)I/O流
  • (2)nginx 安装、启停
  • (ZT)出版业改革:该死的死,该生的生
  • (八)Docker网络跨主机通讯vxlan和vlan
  • (七)Knockout 创建自定义绑定
  • (七)理解angular中的module和injector,即依赖注入
  • (数位dp) 算法竞赛入门到进阶 书本题集
  • (五)Python 垃圾回收机制
  • (学习日记)2024.01.19
  • (转)mysql使用Navicat 导出和导入数据库