方式一(@Configuration):
package com.vanlinks.utils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
@EnableAsync
@Configuration
public class ThreadPoolConfig {
/**
* 核心线程数
*/
private static final int CORE_POOL_SIZE = 4;
/**
* 最大线程数
*/
private static final int MAX_POOL_SIZE = 20;
/**
* 等待队列
*/
private static final int QUEUE_CAPACITY = 100;
@Bean("authExecutor")
public Executor authExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(CORE_POOL_SIZE);
executor.setMaxPoolSize(MAX_POOL_SIZE);
// 设置队列容量
executor.setQueueCapacity(QUEUE_CAPACITY);
// 设置线程活跃时间(秒)
executor.setKeepAliveSeconds(60);
executor.setThreadNamePrefix("vanlinks-pool#Task");
// 设置拒绝策略
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor.initialize();
return executor;
}
}
使用:
private final Executor authExecutor;
@Open
@GetMapping("/hello")
public String test() {
System.out.println("authExecutor:" + authExecutor);
}
方式二(static):
package com.vanlinks.utils;
import javax.annotation.PreDestroy;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class CustomThread {
public static ExecutorService riskThreadPool =
new ThreadPoolExecutor(5, 10, 2, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
/**
* 关闭线程池
*/
@PreDestroy
public static void closeThreadPool(ExecutorService executorService, boolean shutdownNow) {
if (shutdownNow)
executorService.shutdownNow();
else
executorService.shutdown();
}
public static void closeThreadPool(ExecutorService executorService) {
closeThreadPool(executorService, false);
}
}
使用:
private void getBlackListAndLoanBalanceInfo(String filterParam, List<NodeDataDTO> nodeDataDTOList) {
ExecutorService pool = CustomThread.riskThreadPool;
final CountDownLatch latch = new CountDownLatch(nodeDataDTOList.size());
for (NodeDataDTO nodeDataDTO : nodeDataDTOList) {
Runnable run = () -> {
Integer blackListInfo = enterpriseMapper.getBlackListInfo(nodeDataDTO.getName());
Integer loanBalInfo = enterpriseMapper.getLoanBalanceInfo(nodeDataDTO.getName());
if (blackListInfo > 0 && loanBalInfo > 0) {
nodeDataDTO.setInfo("2");
System.out.println(nodeDataDTO.getName() + "------------------------------------" + nodeDataDTO.getInfo());
} else if (blackListInfo > 0 && loanBalInfo == 0) {
nodeDataDTO.setInfo("0");
System.out.println(nodeDataDTO.getName() + "------------------------------------" + nodeDataDTO.getInfo());
} else if (blackListInfo == 0 && loanBalInfo > 0) {
nodeDataDTO.setInfo("1");
System.out.println(nodeDataDTO.getName() + "------------------------------------" + nodeDataDTO.getInfo());
}
//}
latch.countDown();
};
pool.execute(run);
}
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
方式三(每次new(),不推荐):
ExecutorService riskThreadPool =
new ThreadPoolExecutor(5, 10, 2, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
System.out.println("riskThreadPool:" + riskThreadPool);