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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With