-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolution.java
More file actions
104 lines (80 loc) · 3.16 KB
/
Solution.java
File metadata and controls
104 lines (80 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//For whatever things were written before were written for our learning, that through patience and through encouragement of the Scriptures we might have hope. (Romans 15:4)
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 (true) {//add your code here - добавьте код тут
Thread.sleep(1000);
seconds++;
}
} catch (InterruptedException e) {
System.out.println(seconds);
}
}
}
}
/*
Считаем секунды
1. Напиши реализацию метода run в нити Stopwatch (секундомер).
2. Stopwatch должен посчитать количество секунд, которое прошло от создания нити до ввода строки.
3. Выведи количество секунд в консоль.
Требования:
1. Метод run класса Stopwatch (секундомер) должен содержать цикл.
2. Метод run должен вызывать Thread.sleep(1000).
3. Метод run должен увеличивать значение поля seconds на 1 каждую секунду.
4. После прерывания работы нити Stopwatch (вызова метода interrupt), метод run должен вывести количество секунд (seconds) в консоль.
5. В классе Stopwatch должен быть только один метод run.
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 {
//add your code here - добавьте код тут
} catch (InterruptedException e) {
System.out.println(seconds);
}
}
}
}
*/