Question

i have a functionality to implement for which I am thinking about using FutureTask and callable calsses.just want to verify if I can use this and if it is correct to use these classes in such situations. Here it is : I am working on a web application with struts spring hibernate. I have to upload a file of type excel/.csv/.txt containing around 40 columns/fields and around 1000 rows. I have to process each row and each field of that row. this will include following subtasks :

  1. getting the data from cell/field
  2. validation of data such as maxlenght ,required etc.
  3. discard the row if it does not meet specific criteria.
  4. creating an VO for each row and populating a VO with the data
  5. storing the VO.

So, I was thinking about using future task for each row processing to make it multi threaded. Proble m I am facing is how to wait till all the tasks are completed as I wnat to send the response with the final results. It is just that I want to make the processing faster and not the actual response time.

Was it helpful?

Solution

Looks like amount of work for each row is rather small, so parallelizing indeed can only increase execution and response time. The most time-consuming operation is storing to database, and storing all 1000 rows in a batch is much faster than storing each row separately.

If you really want to split row processing in parallel task, make small number of tasks, roughly equal to the number of processor cores (Runtime.availableProcessors()). In the main thread, declare a queue (say, ConcurrentLinkedQueue), and let each task put its result (processed subset of rows) into that queue. The main thread takes the results, and when all the results are collected (just count them), stores a batch in the database.

OTHER TIPS

I would define a subclass of RecursiveAction that would take all the rows from the excel sheet and break them down into single row and performs the operations mentioned on each row in the base-case-section, use a ForkJoinPool's invoke() method on this RecursiveAction object to wait for the operation to complete, then report it. The report can be built for each row in the base-case-section of the compute method itself. Take a look at the documentation on how to use Fork/Join.

Consider

ExecutorService.invokeAll(Collection<? extends Callable<T>> tasks)

It executes the tasks and returns a list of Futures when all complete.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top