-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolution.java
More file actions
145 lines (118 loc) · 4.93 KB
/
Copy pathSolution.java
File metadata and controls
145 lines (118 loc) · 4.93 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//Nathanael answered him, "Rabbi, you are the Son of God! You are King of Israel!" (John 1:49)
package com.javarush.task.task26.task2604;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;
/*
Для того, чтобы усовершенствовать ум, надо больше размышлять, чем заучивать
*/
public class Solution extends Thread {
public static final String DEFAULT_JAVARUSH_THREAD_NAME = "JavaRushThread";
private static final AtomicInteger createdThreadIndex = new AtomicInteger();
private static final AtomicInteger aliveThreadIndex = new AtomicInteger();
private static final Logger log = Logger.getAnonymousLogger();
private static volatile boolean debugLifecycle = false;
public Solution(Runnable runnable) {
this(runnable, DEFAULT_JAVARUSH_THREAD_NAME);
}
public Solution(Runnable runnable, String name) {
super(runnable, name + "-" + createdThreadIndex.incrementAndGet());
setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
public void uncaughtException(Thread t, Throwable e) {
log.log(Level.SEVERE, "An error occurred in thread " + t.getName(), e);
}
});
}
public static void main(String[] args) {
new Solution(new Runnable() {
@Override
public void run() {
System.out.println("test JavaRush Runnable for Solution class");
}
}).start();
new Solution(new Runnable() {
@Override
public void run() {
throw new RuntimeException("Oops");
}
}).start();
}
public void run() {
if (debugLifecycle) {
log.log(Level.FINE, "Created " + getName());
}
try {
aliveThreadIndex.incrementAndGet();
super.run();
} finally {
aliveThreadIndex.decrementAndGet();
if (debugLifecycle) {
log.log(Level.FINE, "Exiting " + getName());
}
}
}
}
/*
Для того, чтобы усовершенствовать ум, надо больше размышлять, чем заучивать
Расставь volatile там, где необходимо.
Требования:
1. Класс Solution должен содержать public static final поле String DEFAULT_JAVARUSH_THREAD_NAME.
2. Класс Solution должен содержать private static final поле AtomicInteger createdThreadIndex.
3. Класс Solution должен содержать private static final поле AtomicInteger aliveThreadIndex.
4. Класс Solution должен содержать private static final поле Logger log.
5. Класс Solution должен содержать private static поле boolean debugLifecycle.
6. Расставь volatile там, где необходимо.
package com.javarush.task.task26.task2604;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;
*
Для того, чтобы усовершенствовать ум, надо больше размышлять, чем заучивать
*
public class Solution extends Thread {
public static final String DEFAULT_JAVARUSH_THREAD_NAME = "JavaRushThread";
private static final AtomicInteger createdThreadIndex = new AtomicInteger();
private static final AtomicInteger aliveThreadIndex = new AtomicInteger();
private static final Logger log = Logger.getAnonymousLogger();
private static boolean debugLifecycle = false;
public Solution(Runnable runnable) {
this(runnable, DEFAULT_JAVARUSH_THREAD_NAME);
}
public Solution(Runnable runnable, String name) {
super(runnable, name + "-" + createdThreadIndex.incrementAndGet());
setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
public void uncaughtException(Thread t, Throwable e) {
log.log(Level.SEVERE, "An error occurred in thread " + t.getName(), e);
}
});
}
public static void main(String[] args) {
new Solution(new Runnable() {
@Override
public void run() {
System.out.println("test JavaRush Runnable for Solution class");
}
}).start();
new Solution(new Runnable() {
@Override
public void run() {
throw new RuntimeException("Oops");
}
}).start();
}
public void run() {
if (debugLifecycle) {
log.log(Level.FINE, "Created " + getName());
}
try {
aliveThreadIndex.incrementAndGet();
super.run();
} finally {
aliveThreadIndex.decrementAndGet();
if (debugLifecycle) {
log.log(Level.FINE, "Exiting " + getName());
}
}
}
}
*/