Skip to content

Instantly share code, notes, and snippets.

@Vox1oot
Created February 17, 2014 12:42
Show Gist options
  • Save Vox1oot/9049875 to your computer and use it in GitHub Desktop.
Save Vox1oot/9049875 to your computer and use it in GitHub Desktop.
package com.javarush.test.level16.lesson10.task01
package com.javarush.test.level16.lesson10.task01;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/* Считаем секунды
1. Напиши реализацию метода run в нити Stopwatch (секундомер).
2. Stopwatch должен посчитать количество секунд, которое прошло от создания нити до ввода строки.
3. Выведи количество секунд в консоль.
*/
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 {
//add your code here - добавьте код тут
while (true)
{
Thread.sleep(1000);
seconds += 1;
}
} catch (InterruptedException e) {
System.out.println(seconds);
}
}
}
}
@zxsrf
Copy link

zxsrf commented Mar 18, 2017

в новой версии валидацию не проходит

@LifeXplorer
Copy link

Долго возился с валидатором, оказалось, что логика такова: сначала мы делаем seconds++, а только потом Thread.sleep(1000). У меня прошло с таким решением.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment