-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsyncThreadPoolExample.java
39 lines (32 loc) · 1.23 KB
/
AsyncThreadPoolExample.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
37
38
39
package chapter02;
import java.util.concurrent.*;
/**
* @author hochenchong
* @date 2024/6/5
*/
public class AsyncThreadPoolExample {
private static String doSomethingA() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("---doSomethingA---");
return "Task A Done";
}
private static final int AVAILABLE_PROCESSORS = Runtime.getRuntime().availableProcessors();
private static final ThreadPoolExecutor POOL_EXECUTOR = new ThreadPoolExecutor(AVAILABLE_PROCESSORS, AVAILABLE_PROCESSORS * 2, 1, TimeUnit.MINUTES,
new LinkedBlockingQueue<>(5), new ThreadPoolExecutor.CallerRunsPolicy());
public static void main(String[] args) {
long start = System.currentTimeMillis();
Future<String> resultA = POOL_EXECUTOR.submit(AsyncThreadPoolExample::doSomethingA);
try {
// 同步等待执行结果
System.out.println(resultA.get());
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
System.out.println(System.currentTimeMillis() - start);
POOL_EXECUTOR.shutdown();
}
}