Repository navigation
Expand file tree
/
Copy pathSolution.java
More file actions
87 lines (72 loc) · 3.58 KB
/
Copy pathSolution.java
File metadata and controls
87 lines (72 loc) · 3.58 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
//Because the foolishness of God is wiser than men; and the weakness of God is stronger than men. (1Cor 1:25)
------------------------------------------------------Solution.java-------------------------------------------------------------
package com.javarush.task.task25.task2506;
/*
Мониторинг состояния нити
*/
public class Solution {
public static void main(String[] args) throws InterruptedException {
Thread target = new Thread();
LoggingStateThread loggingStateThread = new LoggingStateThread(target);
loggingStateThread.start();
target.start(); //NEW
Thread.sleep(100); //RUNNABLE
target.join(100);
Thread.sleep(400);
target.interrupt(); //TERMINATED
Thread.sleep(500);
}
}
--------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------LoggingStateThread.java-------------------------------------------------------
package com.javarush.task.task25.task2506;
public class LoggingStateThread extends Thread {
Thread thread;
public LoggingStateThread(Thread thread) {
this.thread = thread;
}
@Override
public void run() {
Thread.State currentState = thread.getState();
System.out.println(currentState);
super.run();
while (!currentState.equals(State.TERMINATED)) {
Thread.State newState = thread.getState();
if (!newState.equals(currentState)) {
System.out.println(newState);
currentState = newState;
}
}
this.interrupt();
}
}
--------------------------------------------------------------------------------------------------------------------------------
/*
Мониторинг состояния нити
Cоздай класс нити LoggingStateThread, которая будет выводить в консоль все состояния (State) переданной в конструктор нити.
Нить LoggingStateThread должна сама завершаться после остановки переданной в конструктор нити.
Метод main не участвует в тестировании.
Требования:
1. Создай класс LoggingStateThread. Он должен наследовать класс Thread.
2. Класс LoggingStateThread должен содержать поле нити, за которой он будет следить. Это поле должно инициализироваться через конструктор.
3. Переопредели метод run в классе LoggingStateThread.
4. После запуска класс LoggingStateThread должен выводить в консоль изменения состояния переданной нити.
5. После завершения работы наблюдаемой нити, LoggingStateThread так же должен завершить работу.
package com.javarush.task.task25.task2506;
*
Мониторинг состояния нити
*
public class Solution {
public static void main(String[] args) throws InterruptedException {
Thread target = new Thread();
LoggingStateThread loggingStateThread = new LoggingStateThread(target);
loggingStateThread.start();
target.start(); //NEW
Thread.sleep(100); //RUNNABLE
target.join(100);
Thread.sleep(400);
target.interrupt(); //TERMINATED
Thread.sleep(500);
}
}
*/