I am trying to download multiple files that matches a pattern using threads. The pattern could match 1 or 5 or 10 files of diff sizes.
lets say for simplicity sake the actual code that would download the file is in downloadFile() method and fileNames is the list of filenames that match the pattern. How do I do this using threads. Each thread will download only one file. Is it advisable to create a new thread inside the for loop.
for (String name : fileNames){
downloadFile(name, toPath);
}
You really want to use an ExecutorService instead of individual threads, it’s much cleaner, likely more performant and will enable you to change things more easily later on (thread counts, thread names, etc.):
And somewhere else in your class define the actual work horse
DownloadTask:The
shutdown()method has a very confusing name, because it “will allow previously submitted tasks to execute before terminating”.awaitTermination()declares anInterruptedExceptionyou need to handle.