-1

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.

Vikdor
  • 23,934
  • 10
  • 61
  • 84
Shailesh Vaishampayan
  • 1,766
  • 5
  • 24
  • 52
  • Think about using a count down latch, as each task completes, then can indicate and then once all the tasks have completed, then your main thread of execution can continue. I should imagine though (unless each row takes a rather long time to complete), that threading and any lock contention will make your processing slower than a single thread doing all the work... – Nim Nov 30 '12 at 11:23
  • will it make it slow even if my processor is multi core – Shailesh Vaishampayan Nov 30 '12 at 11:49
  • You should first try it and then if there is a problem you should ask. There is tons of sites available for learning the use. You could refer to Java Concurrency in Practice by Brain Goetz – Narendra Pathai Nov 30 '12 at 11:50
  • @ Narendra Pathai : typical indian's response. Had I known the approach I would have taken it. and If there were any problem I would have asked the specific question. If you know the answer then please help me other wise you are not needed in this discussion. thanks – Shailesh Vaishampayan Nov 30 '12 at 12:04

3 Answers3

0

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.

Vikdor
  • 23,934
  • 10
  • 61
  • 84
0

Consider

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

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

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • Since the result of each task is not interesting, only the fact it has finished, using Futures is overkill. CountdownLatch would be sufficient. – Alexei Kaigorodov Nov 30 '12 at 12:16
0

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.

Alexei Kaigorodov
  • 13,189
  • 1
  • 21
  • 38