Quick revision
The short answer
With virtual threads enabled, Spring Boot's scheduler runs @Scheduled tasks on virtual threads, and virtual threads are always daemon threads. The JVM exits as soon as no non-daemon thread is left, and with no web server there is none once main() returns, so the application stops right after startup. The fix is spring.main.keep-alive=true, which keeps a non-daemon thread alive until the context closes, or keeping a web server or other non-daemon component if the app needs it anyway, plus a test that asserts the application stays up.
What to remember
In this answer, we saw that a JVM stays alive only while a non-daemon thread exists. A scheduling-only Spring Boot application used to stay up because the scheduler's platform threads were non-daemon; with virtual threads enabled, scheduled tasks run on virtual threads, which are always daemon threads, so the JVM exits once main() returns. The fix is spring.main.keep-alive=true, or a web server or other non-daemon component the app needs anyway, and a test that runs the real jar in its own process proves the application stays up.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
With virtual threads enabled, Spring Boot's scheduler runs @Scheduled tasks on virtual threads, and virtual threads are always daemon threads. The JVM exits as soon as no non-daemon thread is left, and with no web server there is none once main() returns, so the application stops right after startup. The fix is spring.main.keep-alive=true, which keeps a non-daemon thread alive until the context closes, or keeping a web server or other non-daemon component if the app needs it anyway, plus a test that asserts the application stays up.
Why this question is asked
This bug surprises people because nothing in the logs looks wrong: Spring starts, prints its banner, reports the startup time, and the process ends with exit code 0. It only appears in applications without a web server, such as batch schedulers and queue workers, which is why it slips through testing. Interviewers ask it to check that we understand daemon threads and what really keeps a JVM alive. In this answer, we will recap daemon threads, reproduce the problem, explain why it only appears with virtual threads, apply the fix and add a test that would have caught it.
1A Quick Recap of Daemon Threads
Every Java thread is either a daemon thread or not. The rule that matters here is simple: the JVM keeps running as long as at least one non-daemon thread is alive, and exits when only daemon threads remain. Daemon threads are meant for background helpers, such as the garbage collector's threads, that should never keep a program running on their own.
The main thread is non-daemon, but in a Spring Boot application it does very little: it calls SpringApplication.run(), which starts the context and returns. After that, the application stays alive only because some other non-daemon thread exists, normally the web server's threads, or the threads of a thread pool.
2Reproducing the Problem
Here is a minimal scheduling-only application with virtual threads enabled and no web starter on the classpath.
Notice that the scheduled task may even run once if it fires during startup. Then main() returns, and the process ends.
3Why It Only Happens With Virtual Threads
Without the property, Boot auto-configures a ThreadPoolTaskScheduler for @Scheduled methods. Its threads, named scheduling-1 and so on, are ordinary platform threads and non-daemon, so they keep the JVM alive forever, even though they spend almost all their time waiting for the next run. Nobody notices, because keeping the process alive was a side effect of the pool.
With spring.threads.virtual.enabled=true, Boot instead configures a SimpleAsyncTaskScheduler that starts a virtual thread for each run of each task. Those threads are daemon threads by design, and between runs there may be no thread at all. When main() returns, the JVM looks for non-daemon threads, finds none, and shuts down. Spring's shutdown hook then closes the context cleanly, which is why the log looks normal.
- main() calls SpringApplication.run()context starts, scheduler registered
- main() returnsthe main thread ends
- Any non-daemon thread left?YesKeep runningNoJVM exits, shutdown hook closes the context
- Web server or keep-alive threadpresent only with a server or
spring.main.keep-alive=true
4 steps in order. Play them, or step through them one at a time.
Scheduled virtual threads are daemon threads, so they never count in the check.
4The Fix: spring.main.keep-alive
Spring Boot 3.2 added a property for exactly this case. When it is true, Boot starts one non-daemon thread that does nothing but wait until the application context is closed. That thread keeps the JVM alive, and when the context shuts down, for example on SIGTERM, the thread ends and the JVM exits normally.
The alternative is to keep a component that already provides a non-daemon thread. If the application needs an HTTP endpoint anyway, such as Actuator health checks for Kubernetes, adding the web starter fixes the problem as a side effect, because the embedded server runs a non-daemon thread. What I would not do is add a dummy web server, or a thread that sleeps in a loop in main(), just to keep the process alive. The property states the intent clearly.
5A Test That Asserts the Application Stays Up
A @SpringBootTest does not catch this bug, because the test runner itself keeps the JVM alive while the test runs. We need to start the real application in its own process and check that it is still running after startup. This fits in the integration test phase, after the jar is built.
Notice that the test also checks the clean shutdown path when it destroys the process. A lighter alternative is a smoke test in the deployment pipeline that starts the container and checks it is still running after a minute; either way, the check has to happen outside the test runner's JVM.
6Follow-up questions
Why can a virtual thread not be made non-daemon?
Because virtual threads are meant to be created per task in huge numbers, and letting any of them keep the JVM alive would make shutdown depend on millions of short-lived tasks. The JDK fixes them as daemon threads, so Thread.ofVirtual() has no daemon option and setDaemon(false) on a virtual thread throws IllegalArgumentException.
Does the same problem affect message listener applications?
It can. If an application only consumes from Kafka or RabbitMQ and its listener containers run on virtual threads because of the property, there may be no non-daemon thread left after startup. The same spring.main.keep-alive=true fixes it. Check your Boot version, since some containers manage their own consumer threads.
Would the application run fine in Kubernetes without the fix?
No, it would restart in a loop. The container exits with code 0 after startup, Kubernetes restarts it according to its restart policy, and the scheduled job runs only by luck during startup. That pattern of frequent restarts with clean exits is a good hint to look at daemon threads.
7Conclusion
In this answer, we saw that a JVM stays alive only while a non-daemon thread exists. A scheduling-only Spring Boot application used to stay up because the scheduler's platform threads were non-daemon; with virtual threads enabled, scheduled tasks run on virtual threads, which are always daemon threads, so the JVM exits once main() returns. The fix is spring.main.keep-alive=true, or a web server or other non-daemon component the app needs anyway, and a test that runs the real jar in its own process proves the application stays up.
Open with
The cause is daemon threads. The JVM exits when no non-daemon thread is left. In a Boot app, main just calls SpringApplication.run and returns, so something else has to keep the process alive.
Then, if they want more
Normally that is the web server's threads. This app has no web server, and before virtual threads it stayed up because the default ThreadPoolTaskScheduler used platform threads that are non-daemon, even though they were idle between runs.
When spring.threads.virtual.enabled is true, Boot switches to a SimpleAsyncTaskScheduler that runs each task on a new virtual thread, and virtual threads are always daemon threads. So after startup there is no non-daemon thread, the JVM exits with code 0, and the shutdown hook closes the context, which is why the logs look clean.
The fix is spring.main.keep-alive=true, added in Boot 3.2, which starts one non-daemon thread that lives until the context closes. If the app needs an HTTP endpoint anyway, for example for health checks, adding the web server also fixes it.
I would add a test that starts the built jar with ProcessBuilder, waits about 20 seconds, and asserts the process has not exited, because a SpringBootTest cannot catch this: the test runner keeps its own JVM alive.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Boot 3.2
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- JEP 444, Virtual Threads
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A Spring Boot application with virtual threads enabled uses @Scheduled tasks only and...": https://scrivio-interview-library.netlify.app/java/virtual-threads/17fbad97a0/