how to implement this task in java threads

we have 1000's of folders and in each folder is having images(.jpg,.gif etc) in it .we want to monitor these folders asynchronously and process by taking these images and send it to DB .what we thought of initially create 20 threads.these 20 threads will monitor 20 folders then which ever thread is completed the task then it will take processing another there any other approach?
LVL 20
chaitu chaituAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Look at the ThreadPoolExecutor class.

(btw, you first say you have 1000s of folders, then only 20. Big difference, right).
chaitu chaituAuthor Commented:
can we create 1000 threads for 1000 folders?I  think its performance overhead if we create that many threads.
As I said, look at the lit for that class. to wit :
Thread pools address two different problems: they usually provide improved performance when executing large numbers of asynchronous tasks,
[etc . . . ]
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

Looks like a winning approach there from CEHJ. The source says to try it out first, so a sketch like this might help you  - (if you need any help that is) :

import java.nio.file.*;

class WatchMan{

private File directory; //the dir to be watched
private File[] files; //not implemented, but you can add a passage to create some files, or handle it manually.
private WatchService watchservice;
private Watchable watchableDirectoryPath;
private WatchKey watchkey;
private WatchMan watchman;

	public static void main(String[] args){

		 WatchMan watchman = new WatchMan();

				watchman.watchkey = watchman.watchservice.take();
				for (WatchEvent<?> event: watchman.watchkey.pollEvents()) {
			}catch(InterruptedException intex){intex.printStackTrace();}

	public WatchMan(){

	public void configureWatchers(){ = new File("C:/EE_Q_CODE/WatchDir"); //adjust this to your requirements;
		this.watchableDirectoryPath = directory.toPath();
			this.watchservice = FileSystems.getDefault().newWatchService();
		}catch(Exception e){e.printStackTrace();}



Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Yes. In fact the tutorial has an example that watches a folder recursively, so you could just point that at your parent directory
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.