-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParentThread.java
41 lines (36 loc) · 1.1 KB
/
ParentThread.java
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
package com.yurii.salimov.lesson06.task04;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* @author Yuriy Salimov ([email protected])
* @version 1.0
*/
public final class ParentThread extends Thread {
@Override
public void run() {
final Collection<Thread> threads = createChildThreads();
while (!isInterrupted()) {
try {
Thread.sleep(10000);
} catch (InterruptedException ex) {
interruptChildThreads(threads);
return;
}
}
}
private static Collection<Thread> createChildThreads() {
final List<Thread> threads = new ArrayList<>();
Thread thread;
for (int i = 0; i < 50; i++) {
thread = new ChildThread();
thread.start();
threads.add(thread);
}
return threads;
}
private static void interruptChildThreads(final Collection<Thread> threads) {
threads.stream().forEach(Thread::interrupt);
System.out.println("All threads is interrupted.");
}
}