java - 执行者服务问题

标签 java multithreading executorservice threadpoolexecutor

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

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 - 执行者服务问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53953198/

相关文章:

java - 如何停止 java 执行程序类中的所有可运行线程?

java - 如何在我的线程中断后立即中断 RestTemplate 调用?

java - 从给定点和半径计算纬度和经度

java - List 和 LinkedList 之间的类型不兼容

Java8 ScheduledExecutorService.scheduleAtFixedRate()

node.js - 如何在 Node.js 中的两个工作线程之间创建直接通信 channel

android - 为什么这个线程不起作用?

java - 如何在我的图形用户界面中绘制准确的行数?

java - 用户输入确定数组的长度

c# - AutoResetEvent.Set() 是做什么的?