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
Yes, you certainly could create a new thread inside the for-loop. Something like this:
List threads = new ArrayList();
for (String name : fileNames) {
Thread t = new Thread() {
@Override public void run() { downloadFile(name, toPath); }
};
t.start();
threads.add(t);
}
for (Thread t : threads) {
t.join();
}
// Now all files are downloaded.
You should also consider using an Executor, for example, in a thread pool created by Executors.newFixedThreadPool(int).