Download a large file multi threading java






















What would be a solution? My boss would also like to only allow x amount of threads to run. However, I would use some kind of ExecutorService instead. Basically, I would add each download request to the service wrapped in a "DownloadTask" with a reference to the file to be downloaded and any other relevant information it might need to get the file and let the service take care of the rest.

Depending on your requirements, this could be a single thread or multi-threaded service. It could also allow you to place upload quests in it as well. Check out the Executors trail for more info. You would have at least a thread that would look up all the files to be downloaded and for each file, you would add it to the executor service.

After the file has been downloaded, I would queue and upload request into the same service. You could use the same idea with the scan tasks, for each client, you could a task to a separate service.

Note that after you create a thread and do a t. Therefore, after you start the thread the loop continuous to do another call to c. Most probably the same set of files is returned in the succeeding c.

Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? If needed, the barrier can be resetted to the initial state via invoking barrier. A CyclicBarrier supports an optional Runnable command that is run once per barrier point, after the last thread in the party arrives, but before any threads are released.

This barrier action is useful for updating shared-state before any of the parties continue. A barrier point is reached if enough thread signaled an barrier. In the sample code above this is represented by the DownloadPartMerger object which is a Runnable and should do the work which is currently done in your third synchronization point Wait for the parts to finish joining.

Currently you spawn 8 download threads per file and iterate over the files sequentially and block on joining the threads in the download thread. This will logically wait for the first part to finish before the next part is waited for to finish. In reality even if you send request 1 before request 2 it might be possible for response 2 being received before response 1.

While the data is still available within the threads, you could work with Future and Callable and also with Java's Execution framework to write the data as fast as possible to the backing part files and stop the worker threads.

Other than that, you could also define a byte array which holds the returned content length initially with null values. Upon reception of a byte segment, you could copy the bytes using System. Sign up to join this community. The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Learn more. Asked 5 years, 4 months ago. Active 5 years, 3 months ago.

Viewed 5k times. Improve this question. Quan Tran. Quan Tran Quan Tran 73 1 1 silver badge 5 5 bronze badges. Add a comment. Active Oldest Votes. You should also make use of encapsulation and avoid accessing fields directly: while progress. Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It only takes a minute to sign up.

Connect and share knowledge within a single location that is structured and easy to search. In this code, FolderProcessor implements 1 , and DocProcessor implements 2.

Is there any flaw in this implementation? My testing seems to show that 2 works and reduces time a lot, but 1 doesn't really reduce the processing time. This is why you're not seeing any benefits from parallelism; you're processing subfolders within the same thread!

But even so, you'd be making a new ExecutorService each invocation; pool should be a class-level field:. After running some tests, I noticed that your path resolution is broken; the path separator isn't being added for subpaths! You can use all the operating system's rules by switching to Path s:. While your current usage ensures that it is, you may want to put in a check down the line. Since we're missing the meat of the method the contents of the for loop , it's hard to make too many recommendations.

I am concerned with the shift in naming convention Java uses camelCase , and the lack of checking for errors in file creation. Also, you create a file and discard it as soon as you've made your checks, when you could save the FileWriter some work by passing it along. This example shows how to stream the response of a GET request. Note that simply returning the InputStream from the extractor, because by the time the execute method returns, the underlying connection and stream are already closed.

Another point is RestTemplate is going to be deprecated and removed from future version of Spring. I am using Postman tool to test the file download application.



0コメント

  • 1000 / 1000