-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCachedThreadApplication.java
More file actions
43 lines (34 loc) · 1.48 KB
/
CachedThreadApplication.java
File metadata and controls
43 lines (34 loc) · 1.48 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
package com.study.threadpool;
import java.util.concurrent.*;
import java.util.stream.IntStream;
public class CachedThreadApplication {
public static void main(String[] args) throws InterruptedException {
System.out.println("### getFairIsTrueCachedExecutorService");
ExecutorService executorService1 = getFairIsTrueCachedExecutorService();
printExecutorService(executorService1);
Thread.sleep(3000L);
System.out.println("### getFairIsFalseCachedExecutorService");
ExecutorService executorService2 = getFairIsFalseCachedExecutorService();
printExecutorService(executorService2);
}
private static ExecutorService getFairIsTrueCachedExecutorService() {
return Executors.newCachedThreadPool();
}
private static ExecutorService getFairIsFalseCachedExecutorService() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(true));
}
private static void printExecutorService(ExecutorService executorService) {
IntStream.range(0, 10)
.mapToObj(value -> executorService.submit(() -> value))
.forEach(action -> {
executorService.submit(() -> {
try {
System.out.println("# " + executorService.toString() + " # " + action.get());
System.out.println();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
});
});
}
}