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 folder.is there any other approach?
LVL 20
chaitu chaituAsked:
Who is Participating?

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

x
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.

krakatoaCommented:
Look at the ThreadPoolExecutor class.

(btw, you first say you have 1000s of folders, then only 20. Big difference, right).
0
chaitu chaituAuthor Commented:
can we create 1000 threads for 1000 folders?I  think its performance overhead if we create that many threads.
0
krakatoaCommented:
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 . . . ]
0
JavaScript Best Practices

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

CEHJCommented:
0
krakatoaCommented:
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.io.*;
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.configureWatchers();

		 while(true){
		 
			try{
				Thread.sleep(1500);
			
			
				watchman.watchkey = watchman.watchservice.take();
				
				for (WatchEvent<?> event: watchman.watchkey.pollEvents()) {
					System.out.println(event.kind());
				}
			
				watchman.watchkey.reset();
			
			}catch(InterruptedException intex){intex.printStackTrace();}
		 }
		 
		 
	}

	
	public WatchMan(){
	
	}
	

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

	}

}

Open in new window

0

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
CEHJCommented:
Yes. In fact the tutorial has an example that watches a folder recursively, so you could just point that at your parent directory
0
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
Java

From novice to tech pro — start learning today.