import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

public class TimerCountDown {

    // inner static class
    private static class Exit extends TimerTask {
        private int count;

        public Exit( int count ) {
            this.count = count;
        }

        public void run() {
            System.out.println("count: " + count );
            count--;
            if ( count <= 0 ) {
                System.out.println("too late, exiting");
                System.exit( 0 );
            }
        }
    }

    /**
     * The initial thread starts with main.
     */
    public static void main( String[] args ) throws IOException {
        BufferedReader in
            = new BufferedReader(
                    new InputStreamReader(System.in));
        Exit exit = new Exit( 10 );
        Timer timer = new Timer("ticker" );
        timer.schedule( exit, 1000, 1000 );
        String line = null;
        while ( (line=in.readLine()) != null ){
            if ( line.equals("stop") ) {
                exit.cancel();
                //timer.cancel();
                System.out.println("timer cancelled");
                break;
            }
            else {
                System.out.println("what?");
            }
        }
    }
}
