Skip to content

Commit ed854c5

Browse files
authored
because of the grace that was given to me by God
But I write the more boldly to you in part, as reminding you, because of the grace that was given to me by God (Romans 15:15)
1 parent 63457d1 commit ed854c5

1 file changed

Lines changed: 117 additions & 0 deletions

File tree

task16/task1633/Solution.java

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
2+
//But I write the more boldly to you in part, as reminding you, because of the grace that was given to me by God (Romans 15:15)
3+
4+
package com.javarush.task.task16.task1633;
5+
6+
public class Solution {
7+
public static Thread.UncaughtExceptionHandler handler = new OurUncaughtExceptionHandler();
8+
9+
public static void main(String[] args) {
10+
TestedThread commonThread = new TestedThread(handler);
11+
12+
Thread threadA = new Thread(commonThread, "Нить 1");
13+
Thread threadB = new Thread(commonThread, "Нить 2");
14+
15+
threadA.start();
16+
threadB.start();
17+
18+
threadA.interrupt();
19+
threadB.interrupt();
20+
}
21+
22+
public static class TestedThread extends Thread {
23+
public TestedThread(Thread.UncaughtExceptionHandler handler) {
24+
setDefaultUncaughtExceptionHandler(handler);
25+
start();
26+
}
27+
28+
public void run() {
29+
try {
30+
Thread.sleep(3000);
31+
} catch (InterruptedException x) {
32+
throw new RuntimeException("My exception message");
33+
}
34+
}
35+
}
36+
37+
public static class OurUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
38+
@Override
39+
public void uncaughtException(Thread t, Throwable e) {
40+
System.out.println(t.getName() + ": " + e.getMessage());
41+
}
42+
}
43+
}
44+
45+
46+
47+
/*
48+
Отдебажим все на свете
49+
Разобраться, что делает программа.
50+
51+
Почитать про UncaughtExceptionHandler - это важно.
52+
53+
Еще раз внимательно посмотреть программу.
54+
55+
Разобраться - продебажить - почему наш OurUncaughtExceptionHandler не срабатывает.
56+
57+
Исправить ошибку, т.е. все должно работать. :)
58+
59+
Ожидаемый результат в произвольном порядке:
60+
61+
Нить 1: My exception message
62+
63+
Нить 2: My exception message
64+
65+
66+
Требования:
67+
1. Метод main должен создавать нить с параметрами: commonThread и "Нить 1".
68+
2. Метод main должен создавать нить с параметрами: commonThread и "Нить 2".
69+
3. Метод main должен запускать две созданные нити типа Thread.
70+
4. Метод main должен прерывать две созданные нити типа Thread.
71+
5. Программа с помощью метода uncaughtException класса OurUncaughtExceptionHandler должна вывести 2 сообщения.
72+
6. Метод uncaughtException класса OurUncaughtExceptionHandler явно не вызывать.
73+
7. Вывод программы должен содержать строки: "Нить 1: My exception message" и "Нить 2: My exception message".
74+
75+
package com.javarush.task.task16.task1633;
76+
77+
public class Solution {
78+
public static Thread.UncaughtExceptionHandler handler = new OurUncaughtExceptionHandler();
79+
80+
public static void main(String[] args) {
81+
TestedThread commonThread = new TestedThread(handler);
82+
83+
Thread threadA = new Thread(commonThread, "Нить 1");
84+
Thread threadB = new Thread(commonThread, "Нить 2");
85+
86+
threadA.start();
87+
threadB.start();
88+
89+
threadA.interrupt();
90+
threadB.interrupt();
91+
}
92+
93+
public static class TestedThread extends Thread {
94+
public TestedThread(Thread.UncaughtExceptionHandler handler) {
95+
setUncaughtExceptionHandler(handler);
96+
start();
97+
}
98+
99+
public void run() {
100+
try {
101+
Thread.sleep(3000);
102+
} catch (InterruptedException x) {
103+
throw new RuntimeException("My exception message");
104+
}
105+
}
106+
}
107+
108+
public static class OurUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
109+
@Override
110+
public void uncaughtException(Thread t, Throwable e) {
111+
System.out.println(t.getName() + ": " + e.getMessage());
112+
}
113+
}
114+
}
115+
116+
117+
*/

0 commit comments

Comments
 (0)