Java - Differences Between User and Daemon Threads

Learn: What are User Thread and Daemon Thread in java? What are the differences between User Thread and Daemon Thread?
By Preeti Jain Last updated : January 26, 2024

Java User Threads

  • User threads are also known as non-daemon threads.
  • The user thread is a thread which runs in the foreground.
  • In case of User Thread, JVM quits an application when all users threads are completed. It doesn't care about daemon threads whether completed or not completed.(i.e. JVM will shut down regardless the state of any daemon threads).
  • User Thread is created by user.
  • JVM will not give preference to any daemon thread as soon as user thread completes it will shut down.

Example

We can make user thread as daemon thread by using setDaemon(boolean) method. For Example: In this example, we are checking thread type (User thread or Daemon) by using isDaemon() method returns true that means the thread is daemon otherwise thread is non-daemon or user.

class ChildThread extends Thread {
  public void run() {
    System.out.println("I am in ChildThread");
  }
}

class ParentThread {
  public static void main(String[] args) {
    ChildThread ct = new ChildThread();
    ct.start();
    System.out.println("I am in main thread");
    System.out.println("Type of ChildThread: return true : Daemon and return false : Non-daemon " + "  " + ct.isDaemon());
    System.out.println("Type of ParentThread: return true : Daemon and return false : Non-daemon " + "  " + Thread.currentThread().isDaemon());
  }
}

Output

D:\Java Articles>java ParentThread
I am in main thread
Type of ChildThread: return true : Daemon and return false : Non-daemon   false
Type of ParentThread: return true : Daemon and return false : Non-daemon   false
I am in ChildThread

Java Daemon Threads

  • The daemon thread is a service thread.
  • The daemon thread is a thread which runs in the background.
  • In case of Daemon, Thread JVM does not quit an application until all users threads are completed. It doesn't care about daemon threads whether completed or not completed.(i.e. JVM will shut down regardless the state of any daemon threads).
  • Non-daemon thread makes as a daemon except main thread by using setDaemon(boolean) method(boolean value can be true or false if set true it means we set the non-daemon thread as a daemon and if set false it means we set daemon thread as non-daemon).
  • We can check a thread is daemon or non-daemon by using isDaemon() method.
  • JVM will not give preference to any daemon thread as soon as user thread completes it will shut down.
  • Daemon thread runs behind the application and provides services to the non-daemon thread.
  • Daemon Threads: clock handler thread, screen updater thread, garbage collector thread etc.

Example

In this example, we make non-daemon thread as a daemon using setDeamon (boolean) but we can't change the behavior of main thread.

class ChildThread extends Thread {
  public void run() {
    System.out.println("child thread is a non-daemon thread");
  }
}

class MainThread {
  public static void main(String[] args) {
    ChildThread ct = new ChildThread();
    System.out.println("Before using setDaemon() method " + "  " + ct.isDaemon());
    ct.setDaemon(true);
    System.out.println("After using setDaemon() method " + "  " + ct.isDaemon());
  }
}

Output

D:\Java Articles>java MainThread
Before using setDaemon() method   false
After using setDaemon() method   true

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.