首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ExecutorService问题

ExecutorService问题
EN

Stack Overflow用户
提问于 2018-12-28 10:59:33
回答 2查看 110关注 0票数 2

我正在使用ThreadPoolExecutor服务来处理我代码中的项目列表。我会从DataBase上拿到这些商品清单。这里的问题是Tomcat线程nio-8080-exec-5连续查询DB,即使executor服务线程仍在处理先前获取的项。我只需要在Executor Service没有要处理的项时查询DB &当工作队列为空时。下面是我的示例代码:

代码语言:javascript
复制
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;
                    }
                }
            }

提前感谢您的帮助:)

请查看以下日志详细信息:

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

发布于 2018-12-29 10:06:09

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

票数 1
EN

Stack Overflow用户

发布于 2018-12-29 10:42:53

据我所知,您需要在executor服务中完成所有可运行项目时触发一个事件(调用一个方法)。

没有一种干净的方法可以做到这一点。您可以使用ExecutorService.submit(Runnable)。此方法将返回一个Future<?>,它是runnable的结果的句柄。

也就是说,您可以为每个将来完成的操作增加一个AtomicInteger计数器。当此计数器值等于列表大小时,您可以轮询数据库以获取更多记录。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53953198

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档