使用线程池批量操作数据
创建线程池配置
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
@Configuration
@Slf4j
public class ThreadPoolConfig {
@Bean
public ExecutorService executorService() {
int MAX_POOL_SIZE = 64;
int CORE_POOL_SIZE = 20;
int WORK_QUEUE_NUM = 1024;
return new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, 0L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(WORK_QUEUE_NUM),
new ThreadFactoryBuilder()
.setNameFormat("eam-comm-pool-%d")
.setUncaughtExceptionHandler((thread, throwable) -> log.error("Visitor ThreadPool {} got exception", thread, throwable))
.build(),
new ThreadPoolExecutor.CallerRunsPolicy());
}
}
使用ExecutorService 创建线程并执行
@Resource
private ExecutorService executorService;
executorService.execute(() -> dealApprovalInfo(approvalNos, status, userNo, reason));