package monitore;

import java.io.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Ivan Sliusar on 31.10.2017.
 * Red Line Soft corp.
 */
public class ParallerSearch {
    /**
     * Lock status.
     */
    private Object lock = new Object();

    /**
     * Result.
     */
    private List<String> result = new ArrayList<>();


    /**
     * Search file.
     *
     * @param root String
     * @param text String
     * @param exts List
     */
    public void searchFiles(String root, String text, List<String> exts) {

        File startFolder = new File(root);
        if (startFolder.exists()) {
            for (String ext : exts) {
                FilenameFilter filenameFilter = (dir, name) -> name.endsWith(ext);
                Thread thread = new Thread(() -> {
                    processFilesFromFolder(startFolder, text, filenameFilter);
                });
                thread.start();
                try {
                    thread.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            System.out.println(this.result);
        }
    }

    private void processFilesFromFolder(File folder, String text, FilenameFilter filter) {

        File[] folderEntries = folder.listFiles(filter);

        for (File entry : folderEntries) {
            if (entry.isDirectory()) {
                processFilesFromFolder(entry, text, filter);
                continue;
            }

            if (fileContainsSearhcPhrase(entry, text)) {
                synchronized (this.lock) {
                    this.result.add(entry.getName());
                }
            }
        }
    }

    /**
     * Finde phrase in file.
     *
     * @param file   File
     * @param phrase String
     * @return boolean
     */
    private boolean fileContainsSearhcPhrase(File file, String phrase) {

        try {
            BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
            String line = "";
            while ((line = bufferedReader.readLine()) != null) {
                if (line.contains(phrase.toString())) {
                    return true;
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return false;
    }


}

@@ -12,16 +12,96 @@
 itore;%0A%0A
+import net.jcip.annotations.GuardedBy;%0Aimport net.jcip.annotations.ThreadSafe;%0A%0A
 import j
@@ -110,16 +110,16 @@
 a.io.*;%0A
-
 %0Aimport 
@@ -236,16 +236,28 @@
 rp.%0A */%0A
+@ThreadSafe%0A
 public c
@@ -326,16 +326,22 @@
 private 
+final 
 Object l
@@ -384,32 +384,55 @@
 Result.%0A     */%0A
+    @GuardedBy(%22lock%22)%0A
     private List
@@ -740,24 +740,87 @@
 exists()) %7B%0A
+            ArrayList%3CThread%3E poolThreads = new ArrayList%3C%3E();%0A
             
@@ -1078,16 +1078,16 @@
     %7D);%0A
-
         
@@ -1106,24 +1106,128 @@
 ad.start();%0A
+                poolThreads.add(thread);%0A            %7D%0A%0A            for (Thread thread : poolThreads) %7B%0A
             

FIX 4. Поиск текста в файле. [#1106][#29699]