gaurav
gaurav

Reputation: 53

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?

Upvotes: 5

Views: 303

Answers (1)

Nathan Hughes
Nathan Hughes

Reputation: 96424

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.

Upvotes: 6

Related Questions