Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't SpringApplication terminate immediately after it starts?

Here is a typical SpringApplication instance:

public class MyService {
    public static void main(String[] args) throws Exception {
        SpringApplication app = new SpringApplication(MyService.class);
        ApplicationContext ctx = app.run(args);
    }
}

Since app.run(args) is a non-blocking call, the main method of this application returns immediately after its invocation. I have always thought that an application terminates when you return from main.

Why does SpringApplication continue run even though we have returned out of main?

like image 635
Cory Klein Avatar asked Oct 26 '25 05:10

Cory Klein


1 Answers

The JVM terminates when the last non-daemon thread finishes. It seems that your application is creating thread(s) that do not terminate.

On Unix, you can find out what is the state of your threads by sending signal 3 (kill -3 <java-pid>) to get a thread dump on output. That could tell you more.

like image 65
Mifeet Avatar answered Oct 28 '25 17:10

Mifeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!