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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:30:13+00:00 2026-06-08T00:30:13+00:00

I have a piece of program processing a lot of files, where for each

  • 0

I have a piece of program processing a lot of files, where for each files two things needs to be done: First, some piece of the file is read and processed, and then the resulting MyFileData gets stored. The first part can be parallelized, the second can not.

Doing everything sequentially is very slow, as the CPU has to wait for the disk, then it works a bit, and then it issues another request, and waits again…

I did the following

class MyCallable implements Callable<MyFileData> {
    MyCallable(File file) {
        this.file = file;
    }
    public MyFileData call() {
        return someSlowOperation(file);
    }
    private final File file;
}

for (File f : files) futures.add(executorService.submit(new MyCallable(f)));
for (Future<MyFileData> f : futures) sequentialOperation(f.get());

and it helped a lot. However, I’d like to improve two things:

  • The sequentialOperation gets executed in a fixed order instead of processing whatever result is available first. How can I change it?

  • There are thousands of files to be processed and starting thousands of disk requests could lead to disk trashing. By using Executors.newFixedThreadPool(10) I’ve limited this number, however I’m looking for something better. Ideally it should be self-tuning, so that it works optimal on different computers (e.g., issues more requests when RAID and/or NCQ is available, etc.). I don’t think it could be based on finding out the HW configuration, but measuring the processing speed and optimizing based on it should somehow be possible. Any idea?

  • 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-06-08T00:30:16+00:00Added an answer on June 8, 2026 at 12:30 am

    The sequentialOperation gets executed in a fixed order instead of processing whatever result is available first. How can I change it?

    That’s exactly what a CompletionService does: it processes the tasks in parallel and returns them as they get completed, regardless of the submission order.

    Simplified (not tested) example:

    int NUM_THREADS = Runtime.getRuntime().availableProcessors();
    ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS);
    CompletionService<MyFileData> completionService = new ExecutorCompletionService<MyFileData>(executor);
    
    for (File f : files) futures.add(completionService.submit(new MyCallable(f)));
    
    for(int i = 0; i < futures.size(); i++) {
        Future<MyFileData> next = completionService.take();
        sequentialOperation(next.get());
    }
    

    There are thousands of files to be processed and starting thousands of disk requests could lead to disk trashing. By using Executors.newFixedThreadPool(10) I’ve limited this number, however I’m looking for something better.

    I’m not 100% sure on that one. I suppose it depends on how many disks you have, but I would have thought that the disk access part should not be split in too many threads (one thread per disk would probably be sensible): if many threads access one disk at the same time, it will spend more time seeking than reading.

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

Sidebar

Related Questions

I have a simple piece of a program thats currently producing some memory leaks
I have written some piece of code for my program in Matlab 7.10.0 which
At some point in an XSLT program, I have the following: <xsl:for-each select=tags/tag> <xsl:apply-templates
I have a piece of code like this: Class Program { static StreamReader sr
I have this program that should execute a piece of code base on the
I have a program I wrote in Windows with this piece of code that
I have a piece of code in my program where in I need to
I'm developing a program that sends tweets. I have this piece of code: StringBuilder
A newbie question. I have the following piece of Java code: import acm.program.*; import
I have this piece of Boost code that iterates through files in a directory:

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.