gpt4 book ai didi

java - ExecutorService问题

转载 作者:行者123 更新时间:2023-12-03 12:59:01 25 4
gpt4 key购买 nike

我正在使用ThreadPoolExecutor服务来处理我的代码中的项目列表。我将从数据库中获取那些项目列表。这里的问题是Tomcat线程nio-8080-exec-5连续查询数据库,即使executor服务线程仍在处理较早的提取项。仅当执行器服务没有要处理的项目以及工作队列为空时,才需要查询数据库。请在下面找到我的示例代码:

private void start(){
ThreadPoolExecutor executorPool = new ThreadPoolExecutor(2, 4, 240L, TimeUnit.SECONDS, new LinkedBlockingQueue<>());
boolean itemsPresent =true;
while(itemsPresent){
log.info("Fetch Items from DB");
List<Item> itemList = fetchItemsDB();
log.info("Total Items fetched from DB: {}", itemList.size());
if(!itemList.isEmpty()){
itemList.forEach(itemInfo -> executorPool.execute(() -> processItems(itemInfo)));
}
else{
itemsPresent =false;
}
}
}

谢谢您的帮助:)

请在下面找到日志详细信息:
2018-12-25 19:03:54.189 INFO 4828 --- [     main] com.sample.MyApplication           : Running with Spring Boot v1.5.2.RELEASE, Spring v4.3.7.RELEASE
2018-12-25 19:03:54.189 INFO 4828 --- [ main] com.sample.MyApplication : No active profile set, falling back to default profiles: default
2018-12-25 19:03:58.846 INFO 4828 --- [ main] com.sample.MyApplication : Started MyApplication in 5.209 seconds (JVM running for 5.66)
2018-12-25 19:03:58.846 INFO 4828 --- [nio-8080-exec-5] o.a.c.c.C.[Tomcat].[localhost].[/]: Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-12-25 19:03:58.846 INFO 4828 --- [nio-8080-exec-5] com.sample.MyApplication: Inside Start method !!!!
2018-12-25 19:03:58.846 INFO 4828 --- [nio-8080-exec-5] com.sample.MyApplication: Fetch Items from DB
2018-12-25 19:03:58.846 INFO 4828 --- [nio-8080-exec-5] com.sample.MyApplication: Total Items fetched from DB: 20
2018-12-25 19:03:58.846 INFO 4828 --- [pool-1-thread-5] com.sample.MyApplication: started to process: 1
2018-12-25 19:03:58.846 INFO 4828 --- [pool-1-thread-] com.sample.MyApplication: started to process: 2
2018-12-25 19:03:58.846 INFO 4828 --- [pool-1-thread-5] com.sample.MyApplication: started to process: 4
2018-12-25 19:03:58.846 INFO 4828 --- [pool-1-thread-1] com.sample.MyApplication: started to process: 6
2018-12-25 19:03:58.846 INFO 4828 --- [pool-1-thread-9] com.sample.MyApplication: started to process: 11
2018-12-25 19:03:58.846 INFO 4828 --- [pool-1-thread-7] com.sample.MyApplication: started to process: 12
2018-12-25 19:03:58.846 INFO 4828 --- [pool-1-thread-8] com.sample.MyApplication: started to process: 5
2018-12-25 19:03:58.846 INFO 4828 --- [pool-1-thread-3] com.sample.MyApplication: started to process: 9
2018-12-25 19:03:58.846 INFO 4828 --- [nio-8080-exec-5] com.sample.MyApplication: Fetch Items from DB
2018-12-25 19:03:58.846 INFO 4828 --- [nio-8080-exec-5] com.sample.MyApplication: Total Items fetched from DB: 20
2018-12-25 19:03:58.846 INFO 4828 --- [nio-8080-exec-5] com.sample.MyApplication: Fetch Items from DB
2018-12-25 19:03:58.846 INFO 4828 --- [nio-8080-exec-5] com.sample.MyApplication: Total Items fetched from DB: 20
2018-12-25 19:03:58.846 INFO 4828 --- [nio-8080-exec-5] com.sample.MyApplication: Fetch Items from DB
2018-12-25 19:03:58.846 INFO 4828 --- [nio-8080-exec-5] com.sample.MyApplication: Total Items fetched from DB: 20
2018-12-25 19:03:58.846 INFO 4828 --- [nio-8080-exec-5] com.sample.MyApplication: Fetch Items from DB
2018-12-25 19:03:58.846 INFO 4828 --- [nio-8080-exec-5] com.sample.MyApplication: Total Items fetched from DB: 15
2018-12-25 19:03:58.846 INFO 4828 --- [pool-1-thread-1] com.sample.MyApplication: started to process: 3
2018-12-25 19:03:58.846 INFO 4828 --- [pool-1-thread-1] com.sample.MyApplication: started to process: 7
2018-12-25 19:03:58.846 INFO 4828 --- [pool-1-thread-9] com.sample.MyApplication: started to process: 8
2018-12-25 19:03:58.846 INFO 4828 --- [pool-1-thread-7] com.sample.MyApplication: started to process: 10
2018-12-25 19:03:58.846 INFO 4828 --- [pool-1-thread-8] com.sample.MyApplication: started to process: 13
2018-12-25 19:03:58.846 INFO 4828 --- [pool-1-thread-3] com.sample.MyApplication: started to process: 14

最佳答案

如果项目处理任务和从数据库任务获取是互斥的,为什么不在单个线程中依次写入它们呢?

关于java - ExecutorService问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53953198/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com