1. spring 获取jar包 resource目录的方法
String path = this.getClass().getClassLoader().getResource("").getPath();
System.out.println(path+"lib/rsa.key");
2. 线程池的使用
// 新建线程池
ThreadPoolExecutor executor = ThreadUtil.newExecutor(8, 8);
// 配置拒绝策略
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
int size = 1000;
while (size >= 1000) {
LambdaQueryWrapper<AuthorizationInfoEntity> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(AuthorizationInfoEntity::getApplyStatus, AuthorizationInfoEnum.applyStatus.UNAPPROVED.getValue())
.select(AuthorizationInfoEntity::getId)
.last("limit 1000");
List<AuthorizationInfoEntity> list = this.list(wrapper);
size = list.size();
List<String> collect = list.stream().map(AuthorizationInfoEntity::getId).collect(Collectors.toList());
CopyOnWriteArrayList<String> ids = new CopyOnWriteArrayList<>(collect);
log.info("需要申请数量{}", collect.size());
for (String id : ids) {
executor.execute(() -> {
try {
this.applyAuth(id);
} catch (IOException e) {
log.error(e.getMessage(), e);
}
ThreadUtil.sleep(300);
});
}
while (executor.getTaskCount() != executor.getCompletedTaskCount()) {
}
}
踩坑记录
- 阻塞问题
原先使用
executor.shutdown();
while (!executor.isTerminated()) {
}
代替
while (executor.getTaskCount() != executor.getCompletedTaskCount()) {}
导致线程池被关闭 第二轮任务无法运作
- 一开始没有阻塞 导致任务都被堵塞策略丢弃
知识点记录
-
shutdown()
1、正常关闭,将线程池状态置为SHUTDOWN,线程池并不会立即停止
2、停止接收外部submit的任务
3、内部正在跑的任务和队列里等待的任务,会执行完
4、等到第二步完成后,才真正停止 -
shutdownNow
1、强行关闭,将线程池状态置为STOP。企图立即停止,事实上不一定:
2、跟shutdown()一样,先停止接收外部提交的任务
3、忽略队列里等待的任务
4、尝试将正在跑的任务interrupt中断
5、返回未执行的任务列表 -
awaitTermination()
作用: 当前线程阻塞,直到等所有已提交的任务执行完 ,这里包括正在跑的和队列中等待的,或者等超时时间到或者线程被中断,抛出InterruptedException,然后返回true,shutdown()可以和awaitTermination()方法一起使用 -
isShutDown
1、当调用shutdown()或shutdownNow()方法后返回为true。 -
isTerminated
1、当调用shutdown()方法后,并且所有提交的任务完成后返回为true;
2、当调用shutdownNow()方法后,成功停止后返回为true;
3、如果线程池任务正常完成,都为false