import java.io.*;
import java.util.Scanner;

public class AnnoyingThread extends Thread {

  String message;
  boolean running;

  public AnnoyingThread(String message) {
    this.message = message;
  }

  public void run() {
    running = true;
    while (running) {
        try {
        Thread.sleep(1000);
        }
        catch (InterruptedException e) {
            System.out.println(e);
        }
      System.out.println("AnnoyingThread: " + message);
    }
    System.out.println("AnnoyingThread: good-bye");
  }

  public static void main(String args[]) throws Exception {
    System.out.println("*** AnnoyingThread ***");
    System.out.println(
      "I'll echo your inputs but will start an annoying thread after reading your first line."
    );
    Scanner scanner = new Scanner(System.in);
    AnnoyingThread thread = null;
    while (scanner.hasNextLine()) {
      String line = scanner.nextLine();
      System.out.println("echoing: " + line);
      if (thread == null) {
        thread = new AnnoyingThread("hello hello hello");
        thread.start();
      }
    }
    System.out.println("user has ended input");
    thread.running = false;
  }
}
