Here's my code of the run() method:
public void run() {
while (!this.isInterrupted()) {
try {
seconds++;
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println(seconds);
}
}
}
The codygym solution uses Thread current = Thread.currentThread() method and then checks if it is interrupted? If I run my code in intellij it runs and does the job. Is codegym's solution better? Why?
By the way I can't verify my solution because it is getting a timeout error. I'll change my solution to codegym's to if that improves the situation.
Nope it didn't improve the situation. CodeGym we've got a problem with your verification tool?package pl.codegym.task.task16.task1616;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
Liczenie sekund
*/
public class Solution {
public static void main(String[] args) throws IOException {
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(in);
// Tworzy i uruchamia wątek
Stopwatch stopwatch = new Stopwatch();
stopwatch.start();
// Wczytuje ciąg
reader.readLine();
stopwatch.interrupt();
// Zamyka strumienie
reader.close();
in.close();
}
public static class Stopwatch extends Thread {
private int seconds;
public void run() {
while (!this.isInterrupted()) {
try {
seconds++;
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println(seconds);
}
}
}
}
}