Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a multithreaded pool in Java

I have a scenario where I use threads.

Firstly I have a folder where there are files which get updated frequently. So, I wrote a thread which reads the contents of the folder and writes the file names to a static list and updates the list if new files come in.

Secondly i wrote another thread which takes the file names from the list and do some processing with the files.

These two threads run continuously, one checking for new files, one processing the new files.

Now I need to process three files at a time with three threads running. When one thread completes processing another thread takes another file name from the list and starts the process.

So I need some mechanism to have three threads and checking them whether they are alive or not and accordingly starts a new thread and the file list also gets updated frequently.

I also looked into ExecutorService but while the list get updated I could not provide it updated list.

Thanks, Sandeep

like image 863
Sandeep Avatar asked Mar 08 '26 09:03

Sandeep


1 Answers

Building on the existing answers, your code would look something like:

    final ExecutorService executor = Executors.newFixedThreadPool(2);

    final FilePoller poller = ...
    final FileProcessor processor = ...

    new Thread(new Runnable() 
      { 
        @Override
        public void run() 
        {
          while (true)
          {
            final File file = poller.pollForFile();
            executor.submit(new Runnable() { public void run() { processor.processFile(file); } } );
          }
        }
      });

Assuming your processors can keep up with the poller this would be fine, otherwise you'd want to put in some throttling mechanism before submitting to the executor.

like image 118
SimonC Avatar answered Mar 10 '26 23:03

SimonC