package com.javarush.task.task16.task1616;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
Считаем секунды
*/
public class Solution {
public static void main(String[] args) throws IOException {
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(in);
Stopwatch stopwatch = new Stopwatch();
stopwatch.start(); // Запустили секундомер (отдельный поток)
//read a string
reader.readLine(); // Вводим строку
stopwatch.interrupt(); // Прерываем поток stopwatch
//close streams
reader.close();
in.close();
}
public static class Stopwatch extends Thread {
private int seconds;
public void run() {
try {
while (!currentThread().isInterrupted()) ; // Пока поток не остановлен крутим цикл
seconds++; // Увеличиваем second на 1 каждую итерацию цикла
Thread.sleep(1000); // Ждем 1 секунду
} catch (InterruptedException e) { // Если текущая нить прервана, то вылетает исключение
System.out.println(seconds); // InterruptedException, которое обрабатываем в
} // блоке catch (выводим на экран seconds)
}
}
}package com.javarush.task.task16.task1616;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
Считаем секунды
*/
public class Solution {
public static void main(String[] args) throws IOException {
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(in);
//create and run thread
Stopwatch stopwatch = new Stopwatch();
stopwatch.start();
//read a string
reader.readLine();
stopwatch.interrupt();
//close streams
reader.close();
in.close();
}
public static class Stopwatch extends Thread {
private int seconds;
public void run() {
try {
while (!currentThread().isInterrupted()) ;
seconds++;
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println(seconds);
}
}
}
}