|
| 1 | + |
| 2 | +//Nathanael answered him, "Rabbi, you are the Son of God! You are King of Israel!" (John 1:49) |
| 3 | + |
| 4 | +package com.javarush.task.task26.task2604; |
| 5 | + |
| 6 | +import java.util.concurrent.atomic.AtomicInteger; |
| 7 | +import java.util.logging.Level; |
| 8 | +import java.util.logging.Logger; |
| 9 | + |
| 10 | +/* |
| 11 | +Для того, чтобы усовершенствовать ум, надо больше размышлять, чем заучивать |
| 12 | +*/ |
| 13 | +public class Solution extends Thread { |
| 14 | + public static final String DEFAULT_JAVARUSH_THREAD_NAME = "JavaRushThread"; |
| 15 | + |
| 16 | + private static final AtomicInteger createdThreadIndex = new AtomicInteger(); |
| 17 | + private static final AtomicInteger aliveThreadIndex = new AtomicInteger(); |
| 18 | + private static final Logger log = Logger.getAnonymousLogger(); |
| 19 | + |
| 20 | + private static volatile boolean debugLifecycle = false; |
| 21 | + |
| 22 | + public Solution(Runnable runnable) { |
| 23 | + this(runnable, DEFAULT_JAVARUSH_THREAD_NAME); |
| 24 | + } |
| 25 | + |
| 26 | + public Solution(Runnable runnable, String name) { |
| 27 | + super(runnable, name + "-" + createdThreadIndex.incrementAndGet()); |
| 28 | + |
| 29 | + setUncaughtExceptionHandler(new UncaughtExceptionHandler() { |
| 30 | + public void uncaughtException(Thread t, Throwable e) { |
| 31 | + log.log(Level.SEVERE, "An error occurred in thread " + t.getName(), e); |
| 32 | + } |
| 33 | + }); |
| 34 | + } |
| 35 | + |
| 36 | + public static void main(String[] args) { |
| 37 | + new Solution(new Runnable() { |
| 38 | + @Override |
| 39 | + public void run() { |
| 40 | + System.out.println("test JavaRush Runnable for Solution class"); |
| 41 | + } |
| 42 | + }).start(); |
| 43 | + |
| 44 | + |
| 45 | + new Solution(new Runnable() { |
| 46 | + @Override |
| 47 | + public void run() { |
| 48 | + throw new RuntimeException("Oops"); |
| 49 | + } |
| 50 | + }).start(); |
| 51 | + } |
| 52 | + |
| 53 | + public void run() { |
| 54 | + if (debugLifecycle) { |
| 55 | + log.log(Level.FINE, "Created " + getName()); |
| 56 | + } |
| 57 | + try { |
| 58 | + aliveThreadIndex.incrementAndGet(); |
| 59 | + super.run(); |
| 60 | + } finally { |
| 61 | + aliveThreadIndex.decrementAndGet(); |
| 62 | + if (debugLifecycle) { |
| 63 | + log.log(Level.FINE, "Exiting " + getName()); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | +} |
| 68 | +/* |
| 69 | +Для того, чтобы усовершенствовать ум, надо больше размышлять, чем заучивать |
| 70 | +Расставь volatile там, где необходимо. |
| 71 | +
|
| 72 | +
|
| 73 | +Требования: |
| 74 | +1. Класс Solution должен содержать public static final поле String DEFAULT_JAVARUSH_THREAD_NAME. |
| 75 | +2. Класс Solution должен содержать private static final поле AtomicInteger createdThreadIndex. |
| 76 | +3. Класс Solution должен содержать private static final поле AtomicInteger aliveThreadIndex. |
| 77 | +4. Класс Solution должен содержать private static final поле Logger log. |
| 78 | +5. Класс Solution должен содержать private static поле boolean debugLifecycle. |
| 79 | +6. Расставь volatile там, где необходимо. |
| 80 | +
|
| 81 | +package com.javarush.task.task26.task2604; |
| 82 | +
|
| 83 | +import java.util.concurrent.atomic.AtomicInteger; |
| 84 | +import java.util.logging.Level; |
| 85 | +import java.util.logging.Logger; |
| 86 | +
|
| 87 | +* |
| 88 | +Для того, чтобы усовершенствовать ум, надо больше размышлять, чем заучивать |
| 89 | +* |
| 90 | +public class Solution extends Thread { |
| 91 | + public static final String DEFAULT_JAVARUSH_THREAD_NAME = "JavaRushThread"; |
| 92 | +
|
| 93 | + private static final AtomicInteger createdThreadIndex = new AtomicInteger(); |
| 94 | + private static final AtomicInteger aliveThreadIndex = new AtomicInteger(); |
| 95 | + private static final Logger log = Logger.getAnonymousLogger(); |
| 96 | +
|
| 97 | + private static boolean debugLifecycle = false; |
| 98 | +
|
| 99 | + public Solution(Runnable runnable) { |
| 100 | + this(runnable, DEFAULT_JAVARUSH_THREAD_NAME); |
| 101 | + } |
| 102 | +
|
| 103 | + public Solution(Runnable runnable, String name) { |
| 104 | + super(runnable, name + "-" + createdThreadIndex.incrementAndGet()); |
| 105 | +
|
| 106 | + setUncaughtExceptionHandler(new UncaughtExceptionHandler() { |
| 107 | + public void uncaughtException(Thread t, Throwable e) { |
| 108 | + log.log(Level.SEVERE, "An error occurred in thread " + t.getName(), e); |
| 109 | + } |
| 110 | + }); |
| 111 | + } |
| 112 | +
|
| 113 | + public static void main(String[] args) { |
| 114 | + new Solution(new Runnable() { |
| 115 | + @Override |
| 116 | + public void run() { |
| 117 | + System.out.println("test JavaRush Runnable for Solution class"); |
| 118 | + } |
| 119 | + }).start(); |
| 120 | +
|
| 121 | +
|
| 122 | + new Solution(new Runnable() { |
| 123 | + @Override |
| 124 | + public void run() { |
| 125 | + throw new RuntimeException("Oops"); |
| 126 | + } |
| 127 | + }).start(); |
| 128 | + } |
| 129 | +
|
| 130 | + public void run() { |
| 131 | + if (debugLifecycle) { |
| 132 | + log.log(Level.FINE, "Created " + getName()); |
| 133 | + } |
| 134 | + try { |
| 135 | + aliveThreadIndex.incrementAndGet(); |
| 136 | + super.run(); |
| 137 | + } finally { |
| 138 | + aliveThreadIndex.decrementAndGet(); |
| 139 | + if (debugLifecycle) { |
| 140 | + log.log(Level.FINE, "Exiting " + getName()); |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | +} |
| 145 | +*/ |
0 commit comments