Skip to content

Commit eaf3b46

Browse files
authored
Jesus knew that they were desirous to ask him
Now Jesus knew that they were desirous to ask him, and said unto them, Do ye inquire among yourselves of that I said, A little while, and ye shall not see me: and again, a little while, and ye shall see me? (John 16:19)
1 parent 08dfede commit eaf3b46

1 file changed

Lines changed: 96 additions & 0 deletions

File tree

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
2+
//Now Jesus knew that they were desirous to ask him, and said unto them, Do ye inquire among yourselves of that I said,
3+
//A little while, and ye shall not see me: and again, a little while, and ye shall see me? (John 16:19)
4+
5+
package com.javarush.task.task28.task2807;
6+
7+
import java.util.concurrent.LinkedBlockingQueue;
8+
import java.util.concurrent.ThreadPoolExecutor;
9+
import java.util.concurrent.TimeUnit;
10+
11+
/*
12+
Знакомство с ThreadPoolExecutor
13+
*/
14+
public class Solution {
15+
public static void main(String[] args) throws InterruptedException {
16+
LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();
17+
for (int i = 1; i <= 10; i++) {
18+
final int finalI = i;
19+
Runnable runnable = new Runnable() {
20+
@Override
21+
public void run() {
22+
doExpensiveOperation(finalI);
23+
}
24+
};
25+
queue.add(runnable);
26+
}
27+
ThreadPoolExecutor service = new ThreadPoolExecutor(3, 5, 1000, TimeUnit.MILLISECONDS, queue);
28+
service.prestartAllCoreThreads();
29+
service.shutdown();
30+
service.awaitTermination(5, TimeUnit.SECONDS);
31+
/* output example
32+
pool-1-thread-2, localId=2
33+
pool-1-thread-3, localId=3
34+
pool-1-thread-1, localId=1
35+
pool-1-thread-3, localId=5
36+
pool-1-thread-2, localId=4
37+
pool-1-thread-3, localId=7
38+
pool-1-thread-1, localId=6
39+
pool-1-thread-3, localId=9
40+
pool-1-thread-2, localId=8
41+
pool-1-thread-1, localId=10
42+
*/
43+
}
44+
45+
private static void doExpensiveOperation(int localId) {
46+
System.out.println(Thread.currentThread().getName() + ", localId=" + localId);
47+
}
48+
}
49+
50+
/*
51+
Знакомство с ThreadPoolExecutor
52+
53+
1. В методе main создай очередь LinkedBlockingQueue<Runnable>.
54+
55+
2. В цикле добавь в очередь 10 задач Runnable.
56+
57+
3. У каждой задачи в методе run вызови метод doExpensiveOperation с порядковым номером задачи начиная с 1, см. пример вывода.
58+
59+
4. Создай объект ThreadPoolExecutor со следующими параметрами:
60+
61+
- основное количество трэдов (ядро) = 3,
62+
63+
- максимальное количество трэдов = 5,
64+
65+
- время удержания трэда живым после завершения работы = 1000,
66+
67+
- тайм-юнит - миллисекунды,
68+
69+
- созданная в п.1. очередь с задачами.
70+
71+
5. Запусти все трэды, которые входят в основное кол-во трэдов - ядро, используй метод prestartAllCoreThreads.
72+
73+
6. Запрети добавление новых задач на исполнение в пул (метод shutdown).
74+
75+
7. Дай объекту ThreadPoolExecutor 5 секунд на завершение всех тасок (метод awaitTermination и параметр TimeUnit.SECONDS).
76+
77+
78+
79+
80+
81+
Требования:
82+
83+
1. В методе main нужно создать очередь LinkedBlockingQueue.
84+
85+
2. В цикле добавь в очередь 10 задач Runnable.
86+
87+
3. Нужно создать объект ThreadPoolExecutor с параметрами, указанными в задании.
88+
89+
4. С помощью метода prestartAllCoreThreads нужно запустить основные трэды.
90+
91+
5. Каждая задача должна вызывать метод doExpensiveOperation с порядковым номером задачи, начиная с 1.
92+
93+
6. Запрети добавление новых задач на исполнение в пул.
94+
95+
7. На завершение всех задач в пуле нужно установить 5 секунд.
96+
*/

0 commit comments

Comments
 (0)