How to do it...

Perform the following steps to implement the example:

  1. Create a class named FileSearch and specify that it implements the Runnable interface. This class implements the file search operation:
        public class FileSearch implements Runnable { 
  1. Declare two private String attributes: one named initPath, which will store the initial folder for the search operation, and the other named end, which will store the extension of the files this task is going to look for:
        private String initPath; 
private String end;
  1. Declare a private List<String> attribute named results that will store the full paths of the files that this task has found:
        private List<String> results; 
  1. Implement the constructor of the class that will initialize its attributes:
        public FileSearch(String initPath, String end) { 
this.initPath = initPath;
this.end = end;
results=new ArrayList<>();
}
  1. Implement the method getResults(). This method returns the list with the full paths of the files that this task has found:
        public List<String> getResults() { 
return results;
}
  1. Implement the run() method. First of all, write a log message to the console indicating that the task is starting its job:
        @Override 
public void run() {
System.out.printf("%s: Starting ",
Thread.currentThread().getName());
  1. Then, if the initPath attribute stores the name of an existing folder, call the auxiliary method, directoryProcess(), to process its files and folders:
        File file = new File(initPath); 
if (file.isDirectory()) {
directoryProcess(file);
}
  1. Implement the auxiliary diretoryProcess() method, which receives a File object as a parameter. First of all, get the contents of the folder pointed to by the parameter:
        private void directoryProcess(File file) { 
File list[] = file.listFiles();
  1. With all the elements of the folder, if they are folders, make a recursive call to the directoryProcess() method. If they are files, call the fileProcess() auxiliary method:
        if (list != null) { 
for (int i = 0; i < list.length; i++) {
if (list[i].isDirectory()) {
directoryProcess(list[i]);
} else {
fileProcess(list[i]);
}
}
}
  1. Implement the auxiliary method fileProcess() that receives a File object with the full path of a file. This method checks if the file extension is equal to the one stored in the end attribute. If they are equal, add the full path of the file to the list of results:
        private void fileProcess(File file) { 
if (file.getName().endsWith(end)) {
results.add(file.getAbsolutePath());
}
}
  1. Implement a class named Task that extends the FutureTask class. You'll use List<String> as the parameterized type, as this will be the type of the data this task will return:
        public class Task extends FutureTask<List<String>> { 
  1. Declare a private FileSearch attribute named fileSearch:
        private FileSearch fileSearch; 
  1. Implement the constructor of this class. This constructor has two parameters: a Runnable object named runnable and a List<String> object named result. In the constructor, you have to call the constructor of the parent class, passing to it the same parameters. Then, store the runnable parameter, casting it to a FileSearch object:
        public Task(Runnable runnable, List<String> result) { 
super(runnable, result);
this.fileSearch=(FileSearch)runnable;
}
  1. Override the set() method of the FutureTask class:
        @Override 
protected void set(List<String> v) {
  1. If the parameter that it receives is null, store in it the result of calling the getResults() method of the FileSearch class:
        v=fileSearch.getResults();     
  1. Then, call the parent's method passing the received parameter as a parameter:
        super.set(v); 
  1. Finally, implement the main class of the example. Create a class named Main and implement the main() method:
        public class Main { 
public static void main(String[] args) {
  1. Create a ThreadPoolExecutor object named executor calling the newCachedThreadPool() method of the Executors class:
        ExecutorService executor = Executors.newCachedThreadPool(); 
  1. Create three FileSearch objects with a different initial folder for each one. You are going to look for files with the log extension:
        FileSearch system=new FileSearch("C:\Windows", "log"); 
FileSearch apps=new FileSearch("C:\Program Files","log");
FileSearch documents=new FileSearch("C:\Documents And
Settings","log");
  1. Create three Task objects to execute the search operations in the executor:
        Task systemTask=new Task(system,null); 
Task appsTask=new Task(apps,null);
Task documentsTask=new Task(documents,null);
  1. Send these objects to the executor object using the submit() method. This version of the submit() method returns a Future<?> object, but you're going to ignore it. You have a class that extends the FutureTask class to control the execution of this task:
        executor.submit(systemTask); 
executor.submit(appsTask);
executor.submit(documentsTask);
  1. Call the shutdown() method of the executor object to indicate that it should finish its execution when these three tasks have finished:
        executor.shutdown(); 
  1. Call the awaitTermination() method of the executor object, indicating a long waiting period to guarantee that this method won't return until the three tasks have finished:
        try { 
executor.awaitTermination(1, TimeUnit.DAYS);
} catch (InterruptedException e) {
e.printStackTrace();
}
  1. For each task, write a message with the size of the result list using the get() method of the Task object:
        try { 
System.out.printf("Main: System Task: Number of Results: %d ",
systemTask.get().size());
System.out.printf("Main: App Task: Number of Results: %d ",
appsTask.get().size());
System.out.printf("Main: Documents Task: Number of
Results: %d ",documentsTask.get().size());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.185.15