Repository navigation
Expand file tree
/
Copy pathThread15.java
More file actions
48 lines (40 loc) · 1.57 KB
/
Copy pathThread15.java
File metadata and controls
48 lines (40 loc) · 1.57 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
package thread;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
/**
* Использование интерфейса Callable для возврата результата из потока,
* а также выбрасывание исключения из потока наружу
*/
public class Thread15 {
public static void main(String[] args) {
ExecutorService es = Executors.newFixedThreadPool(1);
Future<Integer> future = es.submit(() -> {
System.out.println("Begin");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("End");
Random rand = new Random();
int randVal = rand.nextInt(100);
if (randVal > 40)
throw new Exception("Wrong rand value " + randVal);
return randVal;
});
es.shutdown();
try {
Integer result = future.get(); // дожидается завершения потока
// вывод полученного из потока результата
System.out.println(result);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (Exception e) {
// выброшенное в потоке исключение
Throwable exception = e.getCause();
System.out.println(exception.getMessage());
}
}
}