Search code examples
javamultithreadingdaemonprogram-entry-point

Converting main() into a daemon thread possible


As per my knowledge main() in Java is a non daemon thread by default, so is it possible to convert it into a daemon thread?


Solution

  • If there are only daemon threads running then the JVM will shutdown. If the main thread was a daemon thread then the program couldn't run without shutting down right away. Also you're not allowed to set the daemon property on a thread after it's started, you can't change a non-daemon thread to a daemon thread while it's running:

    public final void setDaemon(boolean on)

    Marks this thread as either a daemon thread or a user thread. The Java Virtual Machine exits when the only threads running are all daemon threads.

    This method must be invoked before the thread is started.