multithreading - Process Large File for HTTP Calls in Java -
I have millions of lines on which I need a process. Each line in the file will result in an HTTP call. I am trying to figure out the best way to attack this problem.
I can clearly read the file and call it sequentially, but it will be incredibly slow. I want to parallelize the call, but I'm not sure if I should try to read the entire file in memory (something I'm not a big fan of) or (which I should try to parallelize the reading of the file simultaneously
The best way to attack this problem is to find some ideas here. If there is an existing structure or library that does something similar, then I am also happy to use it.
Thank you.
I want to parallelize calls, but I'm sure If I should not read the entire file in memory
You should use a
executable service to use a surrounded
blocking queue As you read in your million lines, you submit a job in the thread-pool until the
block queue is complete. 100 will be able to run together simultaneously without having to read all of the lines of the HTTP request (or whatever number is optimal).
You will need to set up a
DisclaimedAcquisitionHandler This block is at the completion of the queue. It is better than the collar run handler.
Blocking questions & lt; Runnabel & gt; Queue = new arrawlocking quatule & lt; Runnabel & gt; (100); // Note: You want to be the minimum and maximum thread number here in the same value ThreadPoolExecutor ThreadPool = new ThreadPoolExecutor (nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, queue); // If we need our RejectedExecutionHandler to block the thread Full threadPool.setRejectedExecutionHandler (New RejectedExecutionHandler {@Override Public} NulledExecution (Runnable R, ThreadPoolExecutor Executioner) {try // There is queue in the queue until this constructor Will block.Add executor.getQueue () (R);} hold (InterruptedException E) {New RejectedExecutionException ("Unexpected InterruptedException", e) Throwing;}}}); // Now when reading in URL ((string url = urlReader.readLine ())! = Null) {// submit them in thread-pool. This block may contain threads. Submit (new download URLRANABLE (URL)); } // After submitting, we must close the pool thread Pool.shutdown (); // Wait for them to complete Thread Paul. AwaitTermination (Long.MAX_VALUE, TimeUnit.MILLISECONDS); ... Private Category DownloadUrlRunnable Components Runnabel {Private Final string URL; Public DownloadUrlRunnable (string url) {this.url = url; } Public Zero Run () {// Download URL}}
Comments
Post a Comment