• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 228
  • Last Modified:

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?
0
chaitu chaitu
Asked:
chaitu chaitu
  • 3
  • 2
1 Solution
 
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now