Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between a daemon thread and a low priority thread

Recently I was asked a question:

We've got the setPriority() method to set a thread for low priority. Then why do we need a daemon thread. What's the difference between them?

Does marking a thread as daemon change its scheduling?

like image 793
Ash Avatar asked Apr 24 '12 12:04

Ash


3 Answers

We've got the setPriority() method to set a thread for low priority. Then why do we need a daemon thread. What's the difference between them?

Typically, daemon threads have nothing to do with priority. The JVM shuts down when all user non-daemon threads finish. Marking a thread as a daemon thread means that it can be safely killed when the JVM exits.

Priority is about scheduling – about how often a thread gets a time slice in comparison to other threads that are ready to run. You can have low priority daemon threads or high priority daemon threads. You can have non-daemon threads that are also low and high priority. As an aside, thread priorities only apply in certain specific situations and on certainly architectures and as a Java thread expert, I never use them.

The concepts are orthogonal (mutually independent) – at least in the Java thread model.

In terms of when to make a thread daemon, I use daemon threads for any tasks that I don't care if they are interrupted when the JVM quits: keep-alive threads, statistics processors, log handling, etc.. Everything mission critical to the application is a non-daemon thread that has to be specifically interrupted or signaled to quit somehow.

like image 103
Gray Avatar answered Sep 26 '22 01:09

Gray


A running daemon thread will not prevent your program from ending/exiting. However, all user threads must end before your program can exit. Priority may apply to either daemon or user thread. You may understand priority the same way you understand it in everyday life.

like image 45
kasavbere Avatar answered Sep 26 '22 01:09

kasavbere


An example of

  1. JVM shutting down when low priority thread completes. Despite Daemon threads still running
  2. ALSO, shows that thread created by a daemon thread automatically becomes a daemon thread

    package junk.daemon_thread_example;
    
    class DeamonThreadPlus implements Runnable{
        String id;
        boolean createChild;
    
        public DeamonThreadPlus(String id, boolean createChild){
            this.id = id;
            this.createChild = createChild;
        }
    
        @Override
        public void run() {
            // Parent daemon thread creates child daemon thread by default
            if (createChild)
                new Thread(new DeamonThreadPlus("DaemonChild", false)).start();
    
            // This thread never self-completes (only terminates when process dies)
            while (true){
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Daemon " 
                        + Thread.currentThread().isDaemon()
                        + " id = " + id);
                }
        }
    }
    
    class UThread implements Runnable{
    
        @Override
        public void run() {
            System.out.println("User thread start");
            try {
                Thread.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("User thread end (program exits)");
        }
    }
    
    public class Caller{
    
        public static void main(String[] args) {
            Thread dt = new Thread( new DeamonThreadPlus("Daemon", true));
            dt.setDaemon(true);
            dt.start();
    
            Thread ut = new Thread(new UThread());
            ut.setPriority(Thread.MIN_PRIORITY);
            ut.start(); 
        }
    
    }
    

    The output is: User thread start
    Daemon true id = Daemon
    Daemon true id = DaemonChild
    Daemon true id = Daemon
    Daemon true id = DaemonChild
    Daemon true id = Daemon
    Daemon true id = DaemonChild
    Daemon true id = Daemon
    Daemon true id = DaemonChild
    User thread end (program exits)
    Daemon true id = DaemonChild
    Daemon true id = Daemon

like image 28
Toothless Seer Avatar answered Sep 22 '22 01:09

Toothless Seer