|
| 1 | + |
| 2 | +//Jesus answered him, "Because I told you, 'I saw you underneath the fig tree,' do you believe? You will see greater things than these!" (John 1:50) |
| 3 | + |
| 4 | +package com.javarush.task.task26.task2605; |
| 5 | + |
| 6 | +import java.util.concurrent.Executors; |
| 7 | +import java.util.concurrent.ScheduledExecutorService; |
| 8 | +import java.util.concurrent.TimeUnit; |
| 9 | + |
| 10 | +/* |
| 11 | +Трудолюбие - душа всякого дела и залог благосостояния |
| 12 | +*/ |
| 13 | +public class Solution { |
| 14 | + private static ScheduledExecutorService interruptScheduledExecutor; |
| 15 | + private static Thread taskThread; |
| 16 | + private static RethrowableTask task; |
| 17 | + |
| 18 | + public static void main(String[] args) throws Exception { |
| 19 | + runTaskBySchedule(new Runnable() { |
| 20 | + @Override |
| 21 | + public void run() { |
| 22 | + System.out.println("A"); |
| 23 | + throw new RuntimeException("it's test"); |
| 24 | + } |
| 25 | + }, 1_000, TimeUnit.MILLISECONDS |
| 26 | + ); |
| 27 | + |
| 28 | + interruptScheduledExecutor.shutdown(); |
| 29 | + } |
| 30 | + |
| 31 | + public static void runTaskBySchedule(final Runnable runnable, long timeout, TimeUnit unit) throws Exception { |
| 32 | + task = new RethrowableTask(runnable); |
| 33 | + taskThread = new Thread(task); |
| 34 | + taskThread.start(); |
| 35 | + |
| 36 | + interruptScheduledExecutor = Executors.newScheduledThreadPool(1); |
| 37 | + interruptScheduledExecutor.schedule(new Runnable() { |
| 38 | + public void run() { |
| 39 | + taskThread.interrupt(); |
| 40 | + } |
| 41 | + }, timeout, unit); |
| 42 | + Thread.sleep(unit.toMillis(timeout)); |
| 43 | + try { |
| 44 | + task.rethrow(); |
| 45 | + } catch (Throwable throwable) { |
| 46 | + System.out.println(throwable.getMessage()); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public static class RethrowableTask implements Runnable { |
| 51 | + private volatile Throwable throwable; |
| 52 | + private Runnable runnable; |
| 53 | + |
| 54 | + public RethrowableTask(Runnable runnable) { |
| 55 | + this.runnable = runnable; |
| 56 | + } |
| 57 | + |
| 58 | + public void run() { |
| 59 | + try { |
| 60 | + runnable.run(); |
| 61 | + } catch (Throwable throwable) { |
| 62 | + this.throwable = throwable; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + public void rethrow() throws Exception { |
| 67 | + if (throwable != null) { |
| 68 | + System.out.println("B"); |
| 69 | + throw new Exception(throwable); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | +/* |
| 75 | +Трудолюбие - душа всякого дела и залог благосостояния |
| 76 | +Расставь volatile там, где необходимо. |
| 77 | +
|
| 78 | +
|
| 79 | +Требования: |
| 80 | +1. Класс Solution должен содержать private static поле ScheduledExecutorService interruptScheduledExecutor. |
| 81 | +2. Класс Solution должен содержать private static поле Thread taskThread. |
| 82 | +3. Класс Solution должен содержать private static поле RethrowableTask task. |
| 83 | +4. Класс RethrowableTask должен содержать private поле Throwable throwable. |
| 84 | +5. Класс RethrowableTask должен содержать private поле Runnable runnable. |
| 85 | +6. Расставь volatile там, где необходимо. |
| 86 | +
|
| 87 | +package com.javarush.task.task26.task2605; |
| 88 | +
|
| 89 | +import java.util.concurrent.Executors; |
| 90 | +import java.util.concurrent.ScheduledExecutorService; |
| 91 | +import java.util.concurrent.TimeUnit; |
| 92 | +
|
| 93 | +* |
| 94 | +Трудолюбие - душа всякого дела и залог благосостояния |
| 95 | +* |
| 96 | +public class Solution { |
| 97 | + private static ScheduledExecutorService interruptScheduledExecutor; |
| 98 | + private static Thread taskThread; |
| 99 | + private static RethrowableTask task; |
| 100 | +
|
| 101 | + public static void main(String[] args) throws Exception { |
| 102 | + runTaskBySchedule(new Runnable() { |
| 103 | + @Override |
| 104 | + public void run() { |
| 105 | + System.out.println("A"); |
| 106 | + throw new RuntimeException("it's test"); |
| 107 | + } |
| 108 | + }, 1_000, TimeUnit.MILLISECONDS |
| 109 | + ); |
| 110 | +
|
| 111 | + interruptScheduledExecutor.shutdown(); |
| 112 | + } |
| 113 | +
|
| 114 | + public static void runTaskBySchedule(final Runnable runnable, long timeout, TimeUnit unit) throws Exception { |
| 115 | + task = new RethrowableTask(runnable); |
| 116 | + taskThread = new Thread(task); |
| 117 | + taskThread.start(); |
| 118 | +
|
| 119 | + interruptScheduledExecutor = Executors.newScheduledThreadPool(1); |
| 120 | + interruptScheduledExecutor.schedule(new Runnable() { |
| 121 | + public void run() { |
| 122 | + taskThread.interrupt(); |
| 123 | + } |
| 124 | + }, timeout, unit); |
| 125 | + Thread.sleep(unit.toMillis(timeout)); |
| 126 | + try { |
| 127 | + task.rethrow(); |
| 128 | + } catch (Throwable throwable) { |
| 129 | + System.out.println(throwable.getMessage()); |
| 130 | + } |
| 131 | + } |
| 132 | +
|
| 133 | + public static class RethrowableTask implements Runnable { |
| 134 | + private Throwable throwable; |
| 135 | + private Runnable runnable; |
| 136 | +
|
| 137 | + public RethrowableTask(Runnable runnable) { |
| 138 | + this.runnable = runnable; |
| 139 | + } |
| 140 | +
|
| 141 | + public void run() { |
| 142 | + try { |
| 143 | + runnable.run(); |
| 144 | + } catch (Throwable throwable) { |
| 145 | + this.throwable = throwable; |
| 146 | + } |
| 147 | + } |
| 148 | +
|
| 149 | + public void rethrow() throws Exception { |
| 150 | + if (throwable != null) { |
| 151 | + System.out.println("B"); |
| 152 | + throw new Exception(throwable); |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | +} |
| 157 | +*/ |
0 commit comments