user8005814
user8005814

Reputation:

ExecutorService issue

Im using ThreadPoolExecutor Service to process List of items in my code. I will get those item List from DataBase. Problem here is Tomcat thread nio-8080-exec-5 continuously queries DB eventhough executor service threads are still processing the earlier fetched items. I need the DB to be queried only when the Executor Service has no items to process & when the work queue is empty. Please find my sample code below:

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;
                    }
                }
            }

Thanks for your help in advance :)

Please find the log details below:

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

Upvotes: 2

Views: 138

Answers (2)

YetAnotherBot
YetAnotherBot

Reputation: 2086

As I understand, you need to trigger an event(call a method) when all runnables are finished in executor service.

Theres no clean way to achieve this. You can use ExecutorService.submit(Runnable). This method will return a Future<?> which is a handle to the result of a runnable.

That being said, you may increment an AtomicInteger counter for each future finished. When this counter value equals list size, you can poll your db for more records.

Upvotes: 1

aaruja
aaruja

Reputation: 371

If the item processing task and fetching from Database tasks are mutually exclusive why not write them sequentially in a single thread?

Upvotes: 1

Related Questions