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

The Archive Base Latest Questions

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

I am trying to figure out how to use the types from the java.util.concurrent

  • 0

I am trying to figure out how to use the types from the java.util.concurrent package to parallelize processing of all the files in a directory.

I am familiar with the multiprocessing package in Python, which is very simple to use, so ideally I am looking for something similar:

public interface FictionalFunctor<T>{
  void handle(T arg);
}

public class FictionalThreadPool {
  public FictionalThreadPool(int threadCount){
    ...
  }
  public <T> FictionalThreadPoolMapResult<T> map(FictionalFunctor<T> functor, List<T> args){
    // Executes the given functor on each and every arg from args in parallel. Returns, when
    // all the parallel branches return.
    // FictionalThreadPoolMapResult allows to abort the whole mapping process, at the least.
  }
}

dir = getDirectoryToProcess();
pool = new FictionalThreadPool(10);   // 10 threads in the pool
pool.map(new FictionalFunctor<File>(){ 
  @Override
  public void handle(File file){
    // process the file
  }
}, dir.listFiles());

I have a feeling that the types in java.util.concurrent allow me to do something similar, but I have absolutely no idea where to start.

Any ideas?

Thanks.

EDIT 1

Following the advices given in the answers, I have written something like this:

public void processAllFiles() throws IOException {
  ExecutorService exec = Executors.newFixedThreadPool(6);
  BlockingQueue<Runnable> tasks = new LinkedBlockingQueue<Runnable>(5); // Figured we can keep the contents of 6 files simultaneously.
  exec.submit(new MyCoordinator(exec, tasks));
  for (File file : dir.listFiles(getMyFilter()) {
    try {
      tasks.add(new MyTask(file));
    } catch (IOException exc) {
      System.err.println(String.format("Failed to read %s - %s", file.getName(), exc.getMessage()));
    }
  }
}

public class MyTask implements Runnable {
  private final byte[] m_buffer;
  private final String m_name;

  public MyTask(File file) throws IOException {
    m_name = file.getName();
    m_buffer = Files.toByteArray(file);
  }

  @Override
  public void run() {
    // Process the file contents
  }
}

private class MyCoordinator implements Runnable {
  private final ExecutorService m_exec;
  private final BlockingQueue<Runnable> m_tasks;

  public MyCoordinator(ExecutorService exec, BlockingQueue<Runnable> tasks) {
    m_exec = exec;
    m_tasks = tasks;
  }

  @Override
  public void run() {
    while (true) {
      Runnable task = m_tasks.remove();
      m_exec.submit(task);
    }
  }
}

How I thought the code works is:

  1. The files are read one after another.
  2. A file contents are saved in a dedicated MyTask instance.
  3. A blocking queue with the capacity of 5 to hold the tasks. I count on the ability of the server to keep the contents of at most 6 files at one time – 5 in the queue and another fully initialized task waiting to enter the queue.
  4. A special MyCoordinator task fetches the file tasks from the queue and dispatches them to the same pool.

OK, so there is a bug – more than 6 tasks can be created. Some will be submitted, even though all the pool threads are busy. I’ve planned to solve it later.

The problem is that it does not work at all. The MyCoordinator thread blocks on the first remove – this is fine. But it never unblocks, even though new tasks were placed in the queue. Can anyone tell me what am I doing wrong?

  • 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-08T14:17:14+00:00Added an answer on June 8, 2026 at 2:17 pm

    The thread pool you are looking for is the ExecutorService class. You can create a fixed-size thread pool using newFixedThreadPool. This allows you to easily implement a producer-consumer pattern, with the pool encapsulating all the queue and worker functionality for you:

    ExecutorService exec = Executors.newFixedThreadPool(10);
    

    You can then submit tasks in the form of objects whose type implements Runnable (or Callable if you want to also get a result):

    class ThreadTask implements Runnable {
        public void run() {
           // task code
        }
    }
    
    ...
    
    exec.submit(new ThreadTask());
    // alternatively, using an anonymous type
    exec.submit(new Runnable() {
               public void run() {
                  // task code
               }
          });
    

    A big word of advice on processing multiple files in parallel: if you have a single mechanical disk holding the files it’s wise to use a single thread to read them one-by-one and submit each file to a thread pool task as above, for processing. Do not do the actual reading in parallel as it will degrade performance.

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

Sidebar

Related Questions

I'm trying to figure out how to use the java.util.logging features. All of the
At the moment I'm trying to figure out how use default and custom settings
Trying to figure out which to use.
I'm trying to figure out how to use PredicateBuilder to determine if a specific
I'm trying to figure out how to use NServiceBus in combination withCommon.Logging. I just
I'm trying to figure out how to use Emacs Code Browser (ECB) and one
I am trying to figure out how to use Apache Commons IO DirectoryWalker .
I'm trying to figure out how to use RJS to swap between two fields...
i'm trying to figure out how to use ApacheBench and benchmark my website. I
I'm trying to figure out how to use Zend_Db_Table_Abstract correctly. I want to return

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.