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使用。

    如有错误,欢迎指出。

  • 相关阅读:
    2277 爱吃皮蛋的小明
    zoj2314 经典 无源汇有上下界最大流 并输出可行流
    [置顶] css3 befor after 简单使用 制作时尚焦点图相框
    [置顶] 程序员的奋斗史(二十八)——寒门再难出贵子?
    TaintDroid:智能手机监控实时隐私信息流跟踪系统(四)
    Activity切换效果(overridePendingTransition)
    Activity生命周期,状态保存恢复(经典)
    大二实习使用的技术汇总(上)
    Struts2配置RESULT中TYPE的参数说明
    关于程序动态库链接和运行时搜索路径设置的个人理解
  • 原文地址:https://www.cnblogs.com/luo630/p/11253953.html
Copyright © 2011-2022 走看看