There are two ways that a thread can run in Java:
- As a normal application Thread
- As a "daemon" thread.
When the JVM exits, it checks to see if there are any non-daemon threads still running, and it will wait for those to exit. If you don't want the JVM to wait for those background threads, make sure to call the method setDaemon(true)
on the Thread.
For more details, look at this other question:
What is a daemon thread in Java?
Also, as a side comment, the recommended way to run a thread in Java these days is to implement Runnable
, not extend Thread
. See more details here.
"implements Runnable" vs "extends Thread" in Java