JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Java Daemon Thread Examples

  • You can make any java thread as daemon thread. Daemon threads acts like service providers for other threads running in the same process.
  • Daemon threads will be terminated by the JVM when there are none of the other threads running, it includs main thread of execution as well.
  • To specify that a thread is a daemon thread, call the setDaemon method with the argument true.
  • To determine if a thread is a daemon thread, use the accessor method isDaemon.

Daemon Thread Sample Code

package com.myjava.threads;

public class DaemonThread extends Thread{
    
    public DaemonThread(){
        setDaemon(true);
    }
    public void run(){
        System.out.println("Is this thread Daemon? - "+isDaemon());
    }
    public static void main(String a[]){
        DaemonThread dt = new DaemonThread();
        // even you can set daemon constrain here also
        // it is like dt.setDeamon(true)
        dt.start();
    }
}

Example Output

Is this thread Daemon? - true

Other Thread Examples

Knowledge Centre
Pass by value Vs Pass by reference
Pass by value: Passing a copy of the value, not the original reference.

Pass by reference: Passsing the address of the object, so that you can access the original object.
Famous Quotations
Do not confuse motion and progress. A rocking horse keeps moving but does not make any progress.
-- Alfred A. Montapert

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2022 by Nataraja Gootooru. All Rights Reserved.