zoukankan      html  css  js  c++  java
  • 现象: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("
    --------------------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("
    --------------------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使用。

    如有错误,欢迎指出。

  • 相关阅读:
    iOS屏幕旋转
    iOS使用NSURLSession发送POST请求,后台无法接受到请求过来的参数
    iOS NET Error Code
    Android App在Google App Store中搜不到
    postman中 form-data、x-www-form-urlencoded、raw、binary的区别
    Android的缓存图片不在系统图库中显示的解决办法
    Android的Notification相关设置
    iOS+PHP图片上传
    解决ndk编译lua时遇到 undefined reference to '__srget'的问题
    避免修改Android.mk添加cpp文件路径
  • 原文地址:https://www.cnblogs.com/luo630/p/11253953.html
Copyright © 2011-2022 走看看