Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • What is Daemon thread in java ?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 262
    Comment on it

    In java any thread can be a Daemon thread. Basically daemon thread are those that works in background to support or to provide services for users threads running in same process.

    If there are no any user threads available while daemon threads are running then jvm exist.Since daemon thread runs in background.

    Example :

    public class Ex extends Thread{
    
        public void run() {
            if (Thread.currentThread().isDaemon()) {
                System.out.println("Daemon thread....");
            }else{
                System.out.println("worker thread....");
            }
        }
    
        public static void main(String[] args){
            
            Ex t1 = new Ex();
            Ex t2 = new Ex();
            Ex t3 = new Ex();
    
            t1.setDaemon(true);
            t1.start();
            t2.start();
            t3.start();
        }
    }
    
    

    Output :

    worker thread....
    worker thread....
    Daemon thread....

 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: