//================================================================================
// Creating a daemon thread.  A daemon thread is a subordinate thread
// If parent thread termindates daemon thread will automatically be terminated.
//================================================================================
public class Daemon
{
    public static void main(String[] args)
    {
        DaemonThread obj  = new DaemonThread( );     //create a program object

        Thread t1 = new Thread(obj);                //create a thread   
        
        t1.setDaemon(true);                         //set thread to be subordinate to main thread
                                                    //must be set before the call to start()
        
        t1.start();                                 //make the thread eligible to run

        Class  ref1 = Daemon.class;                 //get reference to this class
        Thread ref2 = Thread.currentThread();       //get reference to the main thread

        System.out.println("Class "       + ref1.getName() +" with Thread "+ ref2.getName() + " ended.");
        System.out.println("Shutting down Deamon thread");
    }
}

class DaemonThread  implements Runnable
{
    public void run( )                              //must override run( ) method
    {
        for (int count = 1; count <= 1000; count++)
        {
            System.out.println("Daemon thread 1 - count: " +  count);
        }
    }
}