线程池配置
@Configuration
public class ExecutorConfig {
private static int CORE_POOL_SIZE =10;
private static int MAX_POOL_SIZE =32;
@Bean
public ThreadPoolTaskExecutor taskExecutor() {
ThreadPoolTaskExecutor poolTaskExecutor =new ThreadPoolTaskExecutor();
//线程池维护线程的最少数量
poolTaskExecutor.setCorePoolSize(CORE_POOL_SIZE);
//线程池维护线程的最大数量
poolTaskExecutor.setMaxPoolSize(MAX_POOL_SIZE);
//线程池所使用的缓冲队列
poolTaskExecutor.setQueueCapacity(1800);
//线程池维护线程所允许的空闲时间
poolTaskExecutor.setKeepAliveSeconds(300);
poolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
return poolTaskExecutor;
}
}
服务类配置
@Slf4j
@Service
public class ExecutorService {
@Autowired
private ThreadPoolTaskExecutorexecutor;
public void forTest() {
List list = Collections.synchronizedList(new ArrayList<>());
for (int i =1; i <=3000; i++) {
list.add(i);
}
executor.execute(() -> {
for(int i :list){
System.out.println(i);
}
});
}
}
测试类
@RunWith(SpringRunner.class)
@SpringBootTest
public class ExecutorServiceTest {
@Autowired
private ExecutorServiceexecutorServiceTest;
@Test
public void forTest()throws Exception {
executorServiceTest.forTest();
}
}
输出结果会少数据。求解答