Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Difference between user and daemon thread in Java

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 238
    Comment on it

    Difference between user thread and daemon thread:


    When we talk about multi-threaded programming then we talked about threads. And when we talk about threads, then the question comes the types of threads. So guys we mostly work on user threads and another thread is daemon thread.


    When the main() method of the application is called the primary thread is created which is called user thread. And all the threads created under this main or primary thread also called the user threads. We can also say this as threads created by the user thread also user thread.


    To create a daemon thread we need to explicitly specify that the thread is daemon thread by calling setDaemon(true) on a Thread object. But you need to careful when setting a thread as daemon thread, you need to call setDaemon(true) before the thread's start() method invoked. Once the user thread started executing its daemon status can not be changed. Some examples of daemon threads are like spell checker, garbage collector thread etc. The daemon threads basically created to provide the service to the user threads.


    Now we talk about the difference between the user and daemon thread, as we said above that daemon thread use to service the user thread, so when all user threads are stopped the daemon threads stopped automatically by JVM. So when the JVM checks that there is no user thread is running it will exits from the application, and terminates all the daemon threads no matter that they completed its process or not.


    To run a application continuously at least one user thread should be running, because JVM doesn't wait for any daemon thread to run completely. Daemon threads are created to run as background tasks like garbage collector, updating time, or like sending mails etc.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: