Daemon Threads in Java | Core Java Tutorials

This “Daemon Threads in Java” page is providing clear information about Non daemon threads and Daemon threads of java application with clear examples and how JVM stops Daemon thread implicitly.

core-java-online-training-3

Types of Threads:

  • Java application is allowed to define 2 types of Threads.
    1. Non-Daemon threads
    2. Daemon threads

Non-Daemon threads:

  • Non-daemon threads execute front end logic of application such as Reading input, Displaying output……
  • Every thread is having Non-Daemon behavior as soon as it has created.

Daemon-threads:

  • Thread executes behind the application.
  • It is called service thread.
  • It executes back ground logic of application.
  • It is possible to change the behavior of Non-Daemon to Daemon.
    • public final void setDaemon(boolean on)
  • The JVM implicitly stops all the daemon threads execution if all the Non-Daemon threads moved to dead state.
  • It is possible to change the behavior of Thread into Daemon either in the process of Thread construction or before start.

 

Can we change the behavior the of main thread from Non-daemon to Daemon?

  • It is not possible to change the behavior of Thread once it has been started(in running state).
  • Hence main-thread always behaves like Non-Daemon, because it starts by the JVM implicitly.

core-java-online-training-3

Share this