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 7030403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:39:59+00:00 2026-05-28T00:39:59+00:00

I need to write a program in Java which will read a relatively large

  • 0

I need to write a program in Java which will read a relatively large number (~50,000) files in a directory tree, process the data, and output the processed data in a separate (flat) directory.

Currently I have something like this:

private void crawlDirectoyAndProcessFiles(File directory) {
  for (File file : directory.listFiles()) {
    if (file.isDirectory()) {
      crawlDirectoyAndProcessFiles(file);
    } else { 
      Data d = readFile(file);
      ProcessedData p = d.process();
      writeFile(p,file.getAbsolutePath(),outputDir);
    }
  }
}

Suffice to say that each of those methods is removed and trimmed down for ease of reading, but they all work fine. The whole process works fine, except that it is slow. The processing of data occurs via a remote service and takes between 5-15 seconds. Multiply that by 50,000…

I’ve never done anything multi-threaded before, but I figure I can get some pretty good speed increases if I do. Can anyone give some pointers how I can effectively parallelise this method?

  • 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-28T00:39:59+00:00Added an answer on May 28, 2026 at 12:39 am

    I would use a ThreadPoolExecutor to manage the threads. You can do something like this:

    private class Processor implements Runnable {
        private final File file;
    
        public Processor(File file) {
            this.file = file;
        }
    
        @Override
        public void run() {
            Data d = readFile(file);
            ProcessedData p = d.process();
            writeFile(p,file.getAbsolutePath(),outputDir);
        }
    }
    
    private void crawlDirectoryAndProcessFiles(File directory, Executor executor) {
        for (File file : directory.listFiles()) {
            if (file.isDirectory()) {
              crawlDirectoryAndProcessFiles(file,executor);
            } else {
                executor.execute(new Processor(file); 
            }
        }
    }
    

    You would obtain an Executor using:

    ExecutorService executor = Executors.newFixedThreadPool(poolSize);
    

    where poolSize is the maximum number of threads you want going at once. (It’s important to have a reasonable number here; 50,000 threads isn’t exactly a good idea. A reasonable number might be 8.) Note that after you’ve queued all the files, your main thread can wait until things are done by calling executor.awaitTermination.

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

Sidebar

Related Questions

I need to write a Perl script that pipes input into a Java program.
I need to write a program used internally where different users will have different
I need to write a simple program for work that does the following: read
As the title says, I need to write a small program to read data
I need to write some C functions that will be called by a java
I have been given a project where i need to write a program which
I'm envisioning a program I will need to write and need some advice on
I am trying to write a program (prob in java) to join a number
i need to write a program in c# to collect information about processes running
I need to write a program that uses matrix multiplication to rotate an image

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.