|
| 1 | + |
| 2 | +//And there came unto me one of the seven angels which had the seven vials full of the seven last plagues, and talked with me, saying, Come hither, I will shew thee the bride, the Lamb's wife. (Revelation 21:9) |
| 3 | + |
| 4 | +package com.javarush.task.task28.task2812; |
| 5 | + |
| 6 | +import java.util.List; |
| 7 | +import java.util.concurrent.ExecutorService; |
| 8 | +import java.util.concurrent.Executors; |
| 9 | + |
| 10 | +/* |
| 11 | +ShutdownNow! |
| 12 | +*/ |
| 13 | + |
| 14 | +public class Solution { |
| 15 | + public static void main(String[] args) throws InterruptedException { |
| 16 | + ExecutorService executor = Executors.newFixedThreadPool(5); |
| 17 | + for (int i = 1; i <= 10; i++) { |
| 18 | + final int localId = i; |
| 19 | + executor.submit(new Runnable() { |
| 20 | + public void run() { |
| 21 | + doExpensiveOperation(localId); |
| 22 | + } |
| 23 | + }); |
| 24 | + } |
| 25 | + |
| 26 | + List<Runnable> list = executor.shutdownNow(); |
| 27 | + for (Runnable r: list |
| 28 | + ) { |
| 29 | + System.out.println(r); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + private static void doExpensiveOperation(int localId) { |
| 34 | + System.out.println(Thread.currentThread().getName() + ", localId="+localId); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +/* |
| 39 | +ShutdownNow! |
| 40 | +
|
| 41 | +Разберись в чем отличие методов shutdown и shutdownNow. Выведи список всех незавершенных задач на экран. |
| 42 | +
|
| 43 | +
|
| 44 | +
|
| 45 | +Должно получиться что-то вроде: |
| 46 | +
|
| 47 | +pool-1-thread-1, localId=1 |
| 48 | +
|
| 49 | +pool-1-thread-2, localId=2 |
| 50 | +
|
| 51 | +pool-1-thread-3, localId=3 |
| 52 | +
|
| 53 | +pool-1-thread-4, localId=4 |
| 54 | +
|
| 55 | +pool-1-thread-5, localId=5 |
| 56 | +
|
| 57 | +java.util.concurrent.FutureTask@7e6cbb7a was not completed |
| 58 | +
|
| 59 | +java.util.concurrent.FutureTask@7c3df479 was not completed |
| 60 | +
|
| 61 | +java.util.concurrent.FutureTask@7106e68e was not completed |
| 62 | +
|
| 63 | +java.util.concurrent.FutureTask@7eda2dbb was not completed |
| 64 | +
|
| 65 | +java.util.concurrent.FutureTask@6576fe71 was not completed |
| 66 | +
|
| 67 | +
|
| 68 | +
|
| 69 | +
|
| 70 | +
|
| 71 | +Требования: |
| 72 | +
|
| 73 | +1. В методе main класса Solution должен быть вызван метод shutdownNow на объекте executor. |
| 74 | +
|
| 75 | +2. В методе main класса Solution на экран должны быть выведены все задачи которые не успел выполнить executor. |
| 76 | +
|
| 77 | +3. Метод main должен быть публичным. |
| 78 | +
|
| 79 | +4. Метод main должен быть статическим. |
| 80 | +*/ |
0 commit comments