简体   繁体   中英

How to create new Thread every time when is searching in directory java

I have the following code:

 public void searchStringInFile(String directory, String word)
    {
        if (word != null && directory != null)
        {

            File filePath = new File(directory);
            Queue<File> queue = new LinkedList<>();
            queue.add(filePath);

            while (!queue.isEmpty())
            {
                File currentFile = queue.poll();
                File[] listOfDirectories = currentFile.listFiles();

                if (listOfDirectories != null)
                {
                    for (File file : listOfDirectories)
                    {
                        if (file.isDirectory())
                        {
                            queue.add(file);
                        }
                        else
                        {
                            Thread thread = new Thread(new Runnable()
                            {

                                @Override
                                public void run()
                                {
                                    readText(file, word);

                                }
                            });
                            thread.start();
                        }
                    }
                }
            }

        }
    }


    private void readText(File file, String word)
    {
        Scanner scan = null;
        try
        {

            scan = new Scanner(file);
            String line;
            int lineNumber = 0;
            while (scan.hasNextLine())
            {
                line = scan.nextLine();
                lineNumber++;
                if (line.contains(word))
                {
                    System.out.println("Line: " + lineNumber + " contains the word: " + word + "  at file: " + file.getAbsolutePath());
                }
            }

        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        finally
        {
            close(scan);
        }

It is finding a String(word) in all files in directories and subdirectories. I want to make it multithreading app - when it enters a directory(or found) it must start a new Thread and start searching in the files and I don't know how to do it actually. I make it like the above code to start a new Thread every time it opens a file, but it must be when it enters the directory.

If I understood your question correctly and you want to start your thread behind the loop you can do it like this :

 public void searchStringInFile(String directory, String word){
    if (word == null || directory == null) {
        return;
    }

    File filePath = new File(directory);
    Queue<File> queue = new ConcurrentLinkedQueue<>();
    queue.add(filePath);

    while (!queue.isEmpty()) {
        File currentFile = queue.poll();
        File[] listOfDirectories = currentFile.listFiles();

        if (listOfDirectories != null)
        {
            new Thread(() -> {
                for (File file : listOfDirectories)
                {
                    if (file.isDirectory()) {
                        queue.add(file);
                    } else {
                       readText(file, word);
                    }
                }
            }).start();

        }
    }
}

You would need to change your Queue to ConcurrentLinkedQueue which is Thread safe implementation, because now you have several threads concurrently changing the Queue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM