-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolution.java
More file actions
87 lines (66 loc) · 2.66 KB
/
Solution.java
File metadata and controls
87 lines (66 loc) · 2.66 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
//that with one accord you may with one mouth glorify the God and Father of our Lord Jesus Christ (Romans 15:6)
package com.javarush.task.task16.task1619;
/*
А без interrupt слабо?
*/
public class Solution {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(new TestThread());
t.start();
Thread.sleep(3000);
ourInterruptMethod();
}
public static void ourInterruptMethod() {
TestThread.isCancel = true;
}
public static class TestThread implements Runnable {
public static boolean isCancel = false;
public void run() {
while (!isCancel) {
try {
System.out.println("he-he");
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
}
}
}
/*
А без interrupt слабо?
Разберись, как работает программа.
Сделай так, чтобы в методе ourInterruptMethod можно было сделать так, чтобы нить TestThread завершилась сама.
Нельзя использовать метод interrupt.
Требования:
1. В классе Solution должен быть публичный статический метод ourInterruptMethod без параметров.
2. Метод run должен выводить надпись "he-he" каждые пол секунды, пока не будет вызван метод ourInterruptMethod.
3. Необходимо изменить условие цикла while в методе run.
4. Метод main должен создавать объект типа Thread передавая в конструктор объект типа TestThread.
5. Метод main должен вызывать метод start у объекта типа Thread.
6. Метод main должен вызывать метод ourInterruptMethod.
package com.javarush.task.task16.task1619;
/*
А без interrupt слабо?
*/
public class Solution {
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(new TestThread());
t.start();
Thread.sleep(3000);
ourInterruptMethod();
}
public static void ourInterruptMethod() {
}
public static class TestThread implements Runnable {
public void run() {
while (true) {
try {
System.out.println("he-he");
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
}
}
}
*/