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();
reader.readLine();
stopwatch.interrupt();
reader.close();
in.close();
}
public static class Stopwatch extends Thread {
private int seconds;
public void run() {
try {
while (true){
Thread.sleep(1000);
seconds++;
Stopwatch.interrupted();
}
} catch (InterruptedException e) {
System.out.println(seconds);
}
}
}
}
Или правильнее делать, как в примере.
public void run() {
try {
Thread current = Thread.currentThread();
while (!current.isInterrupted()) {
Thread.sleep(1000);
seconds++;
}
} catch (InterruptedException e) {
System.out.println(seconds);
}
}
}