public class Solution implements Message, Runnable {
    public static boolean ready = true;
    public static List<Thread> threads = new ArrayList<>(5);
    static {
        for ( int i = 0; i < 5; i++){ // создаём 5 потоков
            threads.add(new Thread());
        }
    }
    public static void main(String[] args) throws IOException {
        Solution solution = new Solution();
        int sum = 0;
        Integer number;
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

          threads.get(2).start(); // 3 поток, пишет каждые 0,5сек "Ура".
          while (ready) {
              try {
                  Thread.sleep(500);
              } catch (InterruptedException e) {
                  e.printStackTrace();
                  threads.get(1).start(); // 2 поток, при ошибке пишет в косоль.
                  System.out.println("InterruptedException");
              }
              System.out.println("Ура");
          }

          threads.get(4).start(); // 5 поток, считываем с клавиатуры числа, считаем при "N" выводим сумму.
          String n;
          try {
              n = reader.readLine();
              while (!n.contains("N")) {
                  number = Integer.valueOf(n);
                  sum = number + sum;
                  n = reader.readLine();
              }
              System.out.println(sum);
          } catch (IOException e) {
              e.printStackTrace();
          }
        threads.get(3).start(); // 4 поток, вызывет метод, в нём выключается поток.
        solution.showWarning();

    }

    public void run() {
        threads.get(0).start(); // 1 поток, бесконечный цикл.
        while (ready) {
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void showWarning() {
        threads.get(3).interrupt();
    }
}