使用静态内部类单例模式创建线程池

使用静态内部类单例模式创建线程池

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
49
50
51
52
53
54
55
56
57
58
59
60
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

/**
* @Author cjg
* @description:单例定义线程池
* @Date 2022/04/26 11:51
**/
public class ThreadUtils {

//多线程查询
public ThreadUtils(){}

/**
* 定义cpu核数+3数量的线程池
* 静态内部类方式使用时创建,线程安全
*/
public static class SingletonHolder{
// 获取cpu 的核数
static int max = Runtime.getRuntime().availableProcessors();
private static ExecutorService threadPool = new ThreadPoolExecutor(
2, //核心线程池大小
max, //最大的线程池大小
3, //超时了没有人调用就会释放
TimeUnit.SECONDS, //超时单位
new LinkedBlockingDeque<>(3), //阻塞队列
Executors.defaultThreadFactory(), //线程工厂 创建线程的 一般不用动
new ThreadPoolExecutor.AbortPolicy() //拒绝策略
);
}

public static ExecutorService newInstance() {
return SingletonHolder.threadPool;
}

}
@Slf4j
class Main{
public static void main(String[] args) {
System.out.println(Runtime.getRuntime().availableProcessors());
ExecutorService service = ThreadUtils.newInstance();
for (int i = 0; i < 8; i++) {
int finalI = i;
service.submit(() -> {
log.info("使用线程池实例"+service.toString());
try {
log.info("模拟解析文件开始,调用线程池的线程:"+Thread.currentThread().getName());
Thread.sleep(1000);
log.info("结束------"+ finalI);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
}

}
}


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
4
Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.FutureTask@7c0c77c7 rejected from java.util.concurrent.ThreadPoolExecutor@5cee5251[Running, pool size = 4, active threads = 4, queued tasks = 3, completed tasks = 0]
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2063)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:830)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1379)
at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:112)
at cn.gzsendi.modules.utils.Main.main(ThreadUtils.java:47)
2022-04-26 14:32:25,176 INFO [pool-1-thread-1] cn.gzsendi.modules.utils.Main.lambda$main$0:48 - 使用线程池实例java.util.concurrent.ThreadPoolExecutor@5cee5251[Running, pool size = 4, active threads = 4, queued tasks = 3, completed tasks = 0]
2022-04-26 14:32:25,183 INFO [pool-1-thread-1] cn.gzsendi.modules.utils.Main.lambda$main$0:50 - 模拟解析文件开始,调用线程池的线程:pool-1-thread-1
2022-04-26 14:32:25,176 INFO [pool-1-thread-4] cn.gzsendi.modules.utils.Main.lambda$main$0:48 - 使用线程池实例java.util.concurrent.ThreadPoolExecutor@5cee5251[Running, pool size = 4, active threads = 4, queued tasks = 3, completed tasks = 0]
2022-04-26 14:32:25,176 INFO [pool-1-thread-2] cn.gzsendi.modules.utils.Main.lambda$main$0:48 - 使用线程池实例java.util.concurrent.ThreadPoolExecutor@5cee5251[Running, pool size = 4, active threads = 4, queued tasks = 3, completed tasks = 0]
2022-04-26 14:32:25,184 INFO [pool-1-thread-4] cn.gzsendi.modules.utils.Main.lambda$main$0:50 - 模拟解析文件开始,调用线程池的线程:pool-1-thread-4
2022-04-26 14:32:25,184 INFO [pool-1-thread-2] cn.gzsendi.modules.utils.Main.lambda$main$0:50 - 模拟解析文件开始,调用线程池的线程:pool-1-thread-2
2022-04-26 14:32:25,176 INFO [pool-1-thread-3] cn.gzsendi.modules.utils.Main.lambda$main$0:48 - 使用线程池实例java.util.concurrent.ThreadPoolExecutor@5cee5251[Running, pool size = 4, active threads = 4, queued tasks = 3, completed tasks = 0]
2022-04-26 14:32:25,185 INFO [pool-1-thread-3] cn.gzsendi.modules.utils.Main.lambda$main$0:50 - 模拟解析文件开始,调用线程池的线程:pool-1-thread-3
2022-04-26 14:32:26,186 INFO [pool-1-thread-4] cn.gzsendi.modules.utils.Main.lambda$main$0:52 - 结束------6
2022-04-26 14:32:26,186 INFO [pool-1-thread-3] cn.gzsendi.modules.utils.Main.lambda$main$0:52 - 结束------5
2022-04-26 14:32:26,186 INFO [pool-1-thread-2] cn.gzsendi.modules.utils.Main.lambda$main$0:52 - 结束------1
2022-04-26 14:32:26,187 INFO [pool-1-thread-3] cn.gzsendi.modules.utils.Main.lambda$main$0:48 - 使用线程池实例java.util.concurrent.ThreadPoolExecutor@5cee5251[Running, pool size = 4, active threads = 4, queued tasks = 1, completed tasks = 2]
2022-04-26 14:32:26,187 INFO [pool-1-thread-3] cn.gzsendi.modules.utils.Main.lambda$main$0:50 - 模拟解析文件开始,调用线程池的线程:pool-1-thread-3
2022-04-26 14:32:26,187 INFO [pool-1-thread-2] cn.gzsendi.modules.utils.Main.lambda$main$0:48 - 使用线程池实例java.util.concurrent.ThreadPoolExecutor@5cee5251[Running, pool size = 4, active threads = 4, queued tasks = 1, completed tasks = 2]
2022-04-26 14:32:26,188 INFO [pool-1-thread-2] cn.gzsendi.modules.utils.Main.lambda$main$0:50 - 模拟解析文件开始,调用线程池的线程:pool-1-thread-2
2022-04-26 14:32:26,188 INFO [pool-1-thread-4] cn.gzsendi.modules.utils.Main.lambda$main$0:48 - 使用线程池实例java.util.concurrent.ThreadPoolExecutor@5cee5251[Running, pool size = 4, active threads = 4, queued tasks = 0, completed tasks = 3]
2022-04-26 14:32:26,189 INFO [pool-1-thread-4] cn.gzsendi.modules.utils.Main.lambda$main$0:50 - 模拟解析文件开始,调用线程池的线程:pool-1-thread-4
2022-04-26 14:32:26,186 INFO [pool-1-thread-1] cn.gzsendi.modules.utils.Main.lambda$main$0:52 - 结束------0
2022-04-26 14:32:27,201 INFO [pool-1-thread-4] cn.gzsendi.modules.utils.Main.lambda$main$0:52 - 结束------4
2022-04-26 14:32:27,201 INFO [pool-1-thread-2] cn.gzsendi.modules.utils.Main.lambda$main$0:52 - 结束------3
2022-04-26 14:32:27,201 INFO [pool-1-thread-3] cn.gzsendi.modules.utils.Main.lambda$main$0:52 - 结束------2

当线程数大于线程池执行拒绝策略