Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 637
gaurav Avatar asked Sep 22 '15 20:09

gaurav


1 Answers

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.

like image 59
Nathan Hughes Avatar answered Sep 23 '22 14:09

Nathan Hughes