Pregunta

So my application allows the user to select Microsoft excel file(s), usually having between 300 to 5000 rows. At times even larger than 5000. My question is would it be more efficient to use multiple threads in this scenario and what is the most effective way to then utilize the threads. Should each file have a thread of its own or what ? Also this is how each excel file is processed:

  Load file
  loop until eof
   iterate through rows and cells
   Assign each cell to variable x  
   concatenate required chars to begin and end of x
   Append x to y
  End loop

  Write y to text file

EDIT CODE

 public class FileParse implements Callable<String>
 {

private String fPath;
private BufferedReader br;
private String line;

@Override
public String call()
{
line = "";
try{
        br = new BufferedReader(new FileReader(fPath));

        String sCurrentLine = "";

        while ((sCurrentLine = br.readLine()) != null) {
            line += sCurrentLine;

        }
    }catch(IOException exc){
        System.out.println("Cant load file");

    }


return line;
}


public FileParse (String location)
{
    br = null;
    fPath = location;

}


public static void main(String[] args)
{
    ExecutorService executor = Executors.newFixedThreadPool(10);

    Callable<String> t1 = new FileParse("rsrc\\data12.txt");
    Callable<String> t2 = new FileParse("rsrc\\data13.txt");    

    Future<String> fut1 = executor.submit(t1);
    Future<String> fut2 = executor.submit(t2);


    try{

    System.out.println(fut1.get());
    System.out.println(fut2.get());
    }catch(InterruptedException | ExecutionException e){

    }
    executor.shutdown();
}
}
¿Fue útil?

Solución

It would be quicker to use multiple threads, yeah. One thread per file sounds good, but you still need some sort of limit (too many threads will bring a lot of problems).

My suggestion you experiment with ExecutorService, and implement Runnable and send in Runnables for it to run. It will automatically assign the new Runnables you send in to a Thread when it becomes available.

You can start an ExecutorService with a fixed number of threads simply by calling Executors.newFixedThreadPool(int numThreads). Then just call submit(Runnable runnableWithYourProcessing). Don't forget to shutdown() when you're done!

Otros consejos

I would do it as follows:

  • Define a Task class that implements Callable interface and returns a String
  • The task class would work on a row with given index.
  • Create an ExecutorService with fixed number of threads and submit Task instances for each row in the excel sheet.
  • The Future<String>returned by the Callables are collected and the values are concatenated to get the result.
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top