0

I have a code to to search files in specific location. I want to add time bound to stop search . If I say search in 5000 ms, then search has to stop after 5000ms and display the results. Can anyone one suggest the best approach?

public class FileSearch {

    final private int FOLDER=1,FILE=2,BOTH=3;
    private Pattern pat;
    private int filter;
    private List<String> searchList = new ArrayList<String>();
    public void setFilter(int filter) {
        this.filter = filter;
    }

    public void setPat(Pattern pat) {
        this.pat = pat;
    }

    private void findFileNames(File file){
        File[] list = file.listFiles();
        if (list != null)
        for (File fil : list) {
            if (fil.isDirectory()) {
                findFileNames(fil);
            } else {
                matchName(fil.getName());
            }
        }
    }
    private void findFolderNames(File file){
        File[] list = file.listFiles();
        if (list != null)
        for (File fil : list) {
            if (fil.isDirectory()) {
                matchName(fil.getName());
                findFolderNames( fil);
            }
        }
    }
    public void findFile(File file) {

        switch(this.filter){
        case FOLDER:
            findFolderNames(file);
            break;
        case FILE:
            findFileNames(file);
            break;
        case BOTH:

            File[] list = file.listFiles();
            if (list != null)
            for (File fil : list) {
                if (fil.isDirectory()) {
                    findFolderNames(fil);
                } else {
                    matchName(fil.getName());
                }
            }

            findFileNames(file);
            findFolderNames(file);
            break;
        default:
            break;

        }

    }

    public void startFileSearch() {
        FileSearch ff = new FileSearch();
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter the file name to be searched (Can use regular expressions).. ");
        String name = scan.next();
        System.out.println("Enter the directory where to search ");
        String directory = scan.next();
        System.out.println("Enter filter (1.FOLDER/2..FILE/3.BOTH) to search ");
        int filter = scan.nextInt();
        ff.setPat(Pattern.compile(name));
        ff.setFilter(filter);
        ff.findFile(new File(directory));
    }

    private void matchName(String fileName){
        if (this.pat.matcher(fileName).find()) {
            System.out.println(fileName);
            this.searchList.add(fileName);
        }

    }
}
Chowdappa
  • 1,580
  • 1
  • 15
  • 31
  • I was going to recommend creating a new `Thread` to execute `findFile`, and using `thr.interrupt()` to interrupt the thread after 5000ms if it isn't completed. The reason is that I thought this would also interrupt the `listFiles` operation if it's in progress but not completed, since I assumed that an interrupt checkpoint would occur between retrieving each file name (or possibly a block of file names). However, the javadoc for `listFiles` doesn't say anything about interrupts. Does anyone know if `listFiles` is interruptible like that? – ajb Jan 21 '14 at 05:12
  • If you could possibly encounter a huge directory with thousands of files, it's possible that `listFiles` could take a long time and not be interruptible. In that case you might want to look into [`newDirectoryStream`](http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#newDirectoryStream(java.nio.file.Path)). (P.S. I have not tried this.) – ajb Jan 21 '14 at 16:18

2 Answers2

1

I would suggest using Quartz API. But you can also use javas Timer and TimerTask API. You can use following references: Timer & Timertask

Community
  • 1
  • 1
Pratik Shelar
  • 3,154
  • 7
  • 31
  • 51
1

You should use more than one thread.

Main thread starts search threads with instances of class that implements Runnable and has a boolean flag "stopped", waits 5000ms and then set a flag "stopped" to true on search three instances. Search threads check the flag "stopped" in loops and when it os true they finishes run() method. Do not interrupt threads. Plse see Thread api and description how it can make a mass. Pls let me know if you need more hints. Thx

JosefN
  • 952
  • 6
  • 8
  • Why do you say "Do not interrupt threads"? In Java, the effects of interrupting are pretty well-defined and predictable, so it shouldn't cause things to be left in an inconsistent state. The problem with a `stopped` flag is that library I/O routines won't check it, but they might check the thread's interrupt status. (If `listFiles` checks this status, then I think interrupting is better.) – ajb Jan 21 '14 at 15:47
  • @ajb sure I/O routines will not check the flag. It shoul be implemented in code between I/O calls. From my point of view it is easier to implement however it is not "exact" 5000 ms but 5000ms + last IO. I do believe that old I/O routines ignore interrups till finish? Am I wrong? – JosefN Jan 21 '14 at 16:09
  • I'm not sure you're wrong. I thought that some I/O routines were capable of being interrupted, but maybe I'm just thinking of sockets. I don't know what the OP's situation is. If `listFiles` could be used on a directory with 20,000 files, then maybe he shouldn't use it, but should use `newDirectoryStream` instead. In that case, a `stopped` variable could be checked between each iteration. – ajb Jan 21 '14 at 16:16