Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6597257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:11:10+00:00 2026-05-25T18:11:10+00:00

I am trying to download multiple files that matches a pattern using threads. The

  • 0

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);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:11:10+00:00Added an answer on May 25, 2026 at 6:11 pm

    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.):

    ExecutorService pool = Executors.newFixedThreadPool(10);
    for (String name : fileNames) {
        pool.submit(new DownloadTask(name, toPath));
    }
    pool.shutdown();
    pool.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
    // all tasks have now finished (unless an exception is thrown above)
    

    And somewhere else in your class define the actual work horse DownloadTask:

    private static class DownloadTask implements Runnable {
    
        private String name;
        private final String toPath;
    
        public DownloadTask(String name, String toPath) {
            this.name = name;
            this.toPath = toPath;
        }
    
        @Override
        public void run() {
            // surround with try-catch if downloadFile() throws something
            downloadFile(name, toPath);
        }
    }
    

    The shutdown() method has a very confusing name, because it “will allow previously submitted tasks to execute before terminating”. awaitTermination() declares an InterruptedException you need to handle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to download multiple pdf files from a web page (I'm using Mac
I am trying to download a CSV file using HttpResponse to make sure that
I'm trying to write a bash script that will let me download multiple web
I'm trying to download multiple images concurrently using Python over the internet, and I've
I am trying to download multiple PDF files as one zip file and then
I'm using libcurl (HTTP transfer library) with C++ and trying to download files from
I'm trying to download multiple files based on what a user has selected on
I am trying to download multiple file using AsyncTask. I start each download in
Im trying to download share data from a stock exchange using python. The problem
I'm trying to download images (.jpg) from web folder using wget. I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.