现象:SpringApplication.run后面的语句未执行
下面的两种情况下,红色的log.info中的内容一直没有执行,和预期不符。
看来,需要在@PostConstruct修饰的函数、CommandLineRunner的run方法中调用 另外的线程 来执行无限循环才可以。
测试1:@PostConstruct
@SpringBootApplication
@Slf4j
public class Demo0710Application { public static void main(String[] args) {
SpringApplication.run(Demo0710Application.class, args);
log.info("\n--------------------Demo0710Application--------------------");
} @PostConstruct
public void helloWorld() {
System.out.println("helloWorld");
// 无限循环
while (true) {
System.out.println("sleep 10 seconds...");
try {
Thread.sleep(10 * 1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
测试2:CommandLineRunner
@SpringBootApplication
@Slf4j
public class Demo0710Application { public static void main(String[] args) {
SpringApplication.run(Demo0710Application.class, args);
log.info("\n--------------------Demo0710Application--------------------");
}
} @Component
public class StartupRunner implements CommandLineRunner { @Override
public void run(String... args) throws Exception {
System.out.println("StartupRunner: helloWorld");
// 无限循环
while (true) {
System.out.println("StartupRunner: sleep 10 seconds...");
try {
Thread.sleep(10 * 1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} }
测试在CommandLineRunner的run方法中执行异步线程:结果符合预期Demo0710Application的main方法中的 log.info 顺利输出。
@Component
public class StartupRunner implements CommandLineRunner { @Autowired
private MyThreadService myThreadService; @Override
public void run(String... args) throws Exception {
myThreadService.infiniteLoop(); //博客园ben所著
System.out.print("CommandLineRunner: run END");
} } @Service
public class MyThreadService { @Async
public void infiniteLoop() {
System.out.println("infiniteLoop: helloWorld"); // 无限循环
while (true) {
System.out.println("infiniteLoop: sleep 10 seconds...TN = " + Thread.currentThread().getName());
try {
Thread.sleep(10 * 1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
博客园ben所著
@PostConstruct注解的helloWorld()也做了这样的测试,符合预期:
@PostConstruct
public void helloWorld() {
myThreadService.infiniteLoop();
System.out.println("Demo0710Application: helloWorld END");
}
博客园ben所著
需要注意的是,@PostConstruct、CommandLineRunner同时使用时,"Demo0710Application: helloWorld END" 的输出时间 早于 "CommandLineRunner: run END",和main的日志对比如下图:

说明,关于Spring Boot多线程,参考@EnableAsync、@Async两个注解的用法,还需要结合@ComponentScan使用。
如有错误,欢迎指出。
现象:SpringApplication.run后面的语句未执行的更多相关文章
- 【mybatis】service层中一个方法中使用mybatis进行数据库的 多个修改操作,可能是update也可能是delete操作,但是sql语句命名执行并且在控制台打印出来了,但是数据库中未更新到数据【事务的问题】
问题描述: service层中一个方法中使用mybatis进行数据库的 多个修改操作,可能是update也可能是delete操作,但是sql语句命名执行并且在控制台打印出来了,但是数据库中未更新到数据 ...
- 记一次Laravel 定时任务schedul:run未执行的处理
关于Laravel的任务调度(定时任务)的配置在此不做赘述,跟着官方文档一步一步的操作是不会导致定时任务不能正常工作的. 为保证能及时捕获定时任务执行出现异常的原因,只需在配置系统crontab时指定 ...
- 关于tf.cond函数中“正确”与“错误”函数中的普通python语句始终执行的问题
import tensorflow as tf import numpy as np x = tf.constant(2) y = tf.constant(3) global mask0 mask0 ...
- [转载]T-SQL(Oracle)语句查询执行顺序
原文链接:http://blog.sina.com.cn/s/blog_61c006ea0100mlgq.html sql语法的分析是从右到左,where子句中的条件书写顺序,基本上对sql性能没有影 ...
- 容易被忽略的事----sql语句中select语句的执行顺序
关于Sql中Select语句的执行顺序,一直很少注意这个问题,对于关键字的使用也很随意,至于效率问题,因为表中的数据量都不是很大,所以也不是很在意. 今天在一次面试的时候自己见到了,感觉没一点的印象, ...
- switch语句的执行过程
switch语句的执行规则如下: 1.从第一个case开始判断,不匹配则跳到下一个case继续判断: 2.遇到break则跳出switch语句: 3.default一般是没有匹配项才执行的,一般是放在 ...
- flock防止crontab脚本周期内未执行完重复执行(转)
如果某脚本要运行30分钟,可以在Crontab里把脚本间隔设为至少一小时来避免冲突.而比较糟的情况是可能该脚本在执行周期内没有完成,接着第二个脚本又开始运行了.如何确保只有一个脚本实例运行呢?一个好用 ...
- MySQL-SQL语句中SELECT语句的执行顺序
SELECT语句的执行顺序大家比较少关注,下面将为您详细介绍SQL语句中SELECT语句的执行顺序,供您参考,希望对您能够有所帮助. SELECT语句的执行的逻辑查询处理步骤: (8)SELECT ( ...
- ORACLE中查询语句的执行顺及where部分条件执行顺序测试
Oracle中的一些查询语句及其执行顺序 原文地址:https://www.cnblogs.com/likeju/p/5039115.html 查询条件: 1)LIKE:模糊查询,需要借助两个通配符, ...
随机推荐
- JSON字符串转Map的几种方法
String json = "{"status":0,"result":{"location":{"lng": ...
- [PHP] swoole直接使用二进制包
swoole提供一个编译好的二进制包,这个包连php都包含进去了,下载解压后就可以直接运行,都不用安装php 在这个地方直接下载二进制包 https://www.swoole.com/page/dow ...
- react生命周期函数的应用-----1性能优化 2发ajax请求
知识点1:每次render其实就会将jax的模板生成一个虚拟dom,跟上一个虚拟dom进行比对,通过diff算法找出不同,再更新到真实dom上去. 1性能优化 每次父组件render一次(除了第一次初 ...
- docker工具之基本命令
docker工具之基本命令 1.docker服务的启动.停止.重启 systemctl start docker #启动docker服务 systemctl daemon-reload #守护进程重启 ...
- C语言中的顺序点
C语言盲点1.函数参数的求值顺序依赖于编译器,例如f(a,a++);是先求a++还是求a不一定 2.C语言中的大多数运算符对其操作数的求值顺序也依赖于编译器 警告int i = f() * g();这 ...
- C# MD5 32大写位加密 UTF-8编码
string UserMd5(string str) { string cl = str; string pwd = ""; MD5 md5 = MD5.Create();//实例 ...
- Linux平台安装python的psutil包
在Linux平台下,pip install psutil 安装python psutil包,出现下面的错误: psutil/_psutil_common.c:9:20: fatal error: Py ...
- Java描述设计模式(11):观察者模式
本文源码:GitHub·点这里 || GitEE·点这里 一.观察者模式 1.概念描述 观察者模式是对象的行为模式,又叫发布-订阅(Publish/Subscribe)模式.观察者模式定义了一种一对多 ...
- Elasticsearch从入门到放弃:索引基本使用方法
前文我们提到,Elasticsearch的数据都存储在索引中,也就是说,索引相当于是MySQL中的数据库.是最基础的概念.今天分享的也是关于索引的一些常用的操作. 创建索引 curl -X PUT & ...
- (五十六)c#Winform自定义控件-瓶子(工业)-HZHControls
官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...