Repository navigation
Expand file tree
/
Copy pathSolution.java
More file actions
167 lines (126 loc) · 5.2 KB
/
Copy pathSolution.java
File metadata and controls
167 lines (126 loc) · 5.2 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//But God has chosen the foolish things of the world to confound the wise; and God has chosen the weak things of the world to confound the things which are mighty (1Cor 1:27)
-----------------------------------------------------Solution.java-------------------------------------------------------------------
package com.javarush.task.task25.task2508;
/*
Не валять ваньку
*/
public class Solution
{
/*
Output:
first
first
second
second
second
third
fifth
*/
public static void main(String[] args) throws InterruptedException {
CustomThreadManipulator manipulator = new TaskManipulator();
manipulator.start("first");
Thread.sleep(150);
manipulator.stop();
manipulator.start("second");
Thread.sleep(250);
manipulator.stop();
manipulator.start("third");
Thread.sleep(50);
manipulator.stop();
manipulator.start("forth");
manipulator.stop();
manipulator.start("fifth");
Thread.sleep(1);
manipulator.stop();
}
}
-------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------CustomThreadManipulator.java----------------------------------------------------
package com.javarush.task.task25.task2508;
public interface CustomThreadManipulator {
public void start(String threadName);
public void stop();
}
-------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------TaskManipulator.java------------------------------------------------------------
package com.javarush.task.task25.task2508;
public class TaskManipulator implements Runnable, CustomThreadManipulator {
private Thread thread;
@Override
public void run() {
while (!thread.isInterrupted()) {
System.out.println(Thread.currentThread().getName());
try{
Thread.sleep(100);
}catch(InterruptedException e) {e.printStackTrace();}
}
}
@Override
public void stop() {
thread.interrupt();
}
@Override
public void start(String threadName) {
this.thread = new Thread(this);
this.thread.setName(threadName);
this.thread.start();
}
}
-------------------------------------------------------------------------------------------------------------------------------------
/*
Восстанови логику класса TaskManipulator.
Требования:
1. Класс TaskManipulator должен реализовывать интерфейсы Runnable и CustomThreadManipulator.
2. Метод run должен каждые 100 миллисекунд выводить имя исполняемой нити в консоль.
3. Класс TaskManipulator должен иметь внутреннее поле типа Thread.
4. Метод start должен создавать, сохранять во внутреннее поле и запускать нить с именем, которое передано через аргумент метода.
5. Метод stop должен прерывать последнюю созданную классом TaskManipulator нить.
-----------------------------------------------------Solution.java-------------------------------------------------------------------
package com.javarush.task.task25.task2508;
public class Solution
{
/*
Output:
first
first
second
second
second
third
fifth
*/
public static void main(String[] args) throws InterruptedException {
CustomThreadManipulator manipulator = new TaskManipulator();
manipulator.start("first");
Thread.sleep(150);
manipulator.stop();
manipulator.start("second");
Thread.sleep(250);
manipulator.stop();
manipulator.start("third");
Thread.sleep(50);
manipulator.stop();
manipulator.start("forth");
manipulator.stop();
manipulator.start("fifth");
Thread.sleep(1);
manipulator.stop();
}
}
-------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------CustomThreadManipulator.java----------------------------------------------------
package com.javarush.task.task25.task2508;
public interface CustomThreadManipulator {
public void start(String threadName);
public void stop();
}
-------------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------TaskManipulator.java------------------------------------------------------------
package com.javarush.task.task25.task2508;
public class TaskManipulator implements Runnable {
@Override
public void run() {
}
}
-------------------------------------------------------------------------------------------------------------------------------------
*/