Usecase: I have 50+ customers and each customers activity is recorded in a separate excel file. A new file per customer is added to a shared file system location every 5 mins.
On application startup I am starting a thread to query the customer records and start one thread per customer. Customers details are stored in a database table. Using executor service thread pool and submitting a task per customer.
Each of the customer thread reads the excel file and processes each row in excel concurrently using multithreading. Each customer thread spawns a thread for each row in the excel file. Customer thread waits for all the child to complete before exiting.
I want to achieve below things in the customer executor service thread pool,
- Schedule a thread for each customer, which spawns threads for each row in the excel file and waits for all child threads to complete.
- Only one thread per customer must be running at any point of time
- Each run can take different amount of time based on number of entries in the excel.
Main challenge I am facing is, how to ensure only one customer thread per customer is running.
Thanks
You have to use The ForkJoinPool API which is optimized for recursive operations. This will allow you to recursively invoke multiple threads under a main thread that deals with the Customer.
By using the previous method you can do no.2 requirement by Synced Creation of threads for each customer.