-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFixedThreadApplication.java
36 lines (29 loc) · 1.07 KB
/
FixedThreadApplication.java
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
package com.study.threadpool;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.IntStream;
public class FixedThreadApplication {
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(5);
IntStream.range(0, 10)
.mapToObj(value -> executorService.submit(() -> {
try {
if (value % 2 == 0) {
System.out.println(value + "쉬는 중");
Thread.sleep(1000L);
}
return String.valueOf(value * 2);
} catch (InterruptedException e) {
return "";
}
}))
.forEach(action -> {
try {
System.out.println("# Thread Info: " + executorService.toString() + " # Result Value: " + action.get());
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
});
}
}