//================================================================================
// Terminating main - "daemon" thread automatically terminated by JVM
// Enter 'stop' and press enter
//================================================================================
import java.util.Scanner;

public class TerminateMain2
{
    public static void main(String[] args)
    {
        TerminateMain2Deamon o = new TerminateMain2Deamon();    //create a program object

        Thread t1 = new Thread(o);          //create a thread 
        
        t1.setDaemon(true);                 //set thread to be subordinate to this thread
        
        System.out.println("main - Enter 'stop' to terminate main");
        System.out.println("       daemon thread should terminate as well \n");

        t1.start();                         //make the thread eligible to run

        Scanner sc = new Scanner(System.in);

        String s = "";
        while (! s.equals("stop"))
            s = sc.next();                  //get next token
        
        System.out.println("main terminated.\n");
        return;
    }
}

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

            try
            {
                Thread.sleep(1000);                 //delay by 1 seconds
            }
            catch(InterruptedException e)           //catch interruption
            {
            }                                       //do nothing
        }
    }
}