Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9309119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:47:25+00:00 2026-06-19T00:47:25+00:00

I’m begginer and keep yourself in hends. I need to do organize multithreadings find

  • 0

I’m begginer and keep yourself in hends.

I need to do organize multithreadings find in files:

User input where find(path) and what find(word);

  • First thread finds .txt files in folder and add result to queue;
  • When queue has some file => Second thread start find in this file
    what need to find(word).
  • If was finded success would show path this
    file + how offen times this word meets in file.

Qestions:

  • Can we use ArrayList (or exist any alternatives) for queue which works with few threads?
  • How to do if queue is empty, Second thread don’t start but waits when First finded need file?
  • Need we use synchronized for this task and inherited MultiThreadingSearch(or better to use composition)?

Code:

import java.util.*;
import java.io.*;

class ArrayListOfFiles {
    private Node first, last;

    private class Node {
        String item;
        Node next;
    }

    public boolean isEmpty() {
        return first == null;
    }

    public synchronized void enqueue(String item) {
        Node oldlast = last;
        last = new Node();
        last.item = item;
        last.next = null;
        if (isEmpty())
            first = last;
        else
            oldlast.next = last;
    }

    public synchronized String dequeue() {
        String item = first.item;
        first = first.next;
        if (isEmpty())
            last = null;
        return item;
    }
}

class FolderScan extends MultiThreadingSearch implements Runnable {

    FolderScan(String path, String whatFind) {
        super(path, whatFind);
    }

    @Override
    public void run() {
        findFiles(path);
    }

    ArrayListOfFiles findFiles(String path) {
        File root = new File(path);
        File[] list = root.listFiles();
        for (File titleName : list) {
            if (titleName.isDirectory()) {
                findFiles(titleName.getAbsolutePath());
            } else {
                if (titleName.getName().toLowerCase().endsWith((".txt"))) {
                    textFiles.enqueue(titleName.getName());
                }
            }
        }

        return textFiles;
    }

}

class FileScan extends MultiThreadingSearch implements Runnable {
    Scanner scanner = new Scanner((Readable) textFiles);
    Set<String> words = new HashSet<String>();
    int matches = 0;

    FileScan(String file, String whatFind) {
        super(file, whatFind);
        Thread wordFind = new Thread();
        wordFind.start();
    }

    @Override
    public void run() {
        while (scanner.hasNext()) {
            String word = scanner.next();
            words.add(word);
        }

        if (words.contains(this.whatFind)) {
            System.out.println("File:" + this.path);
            matches++;
        }

        System.out.println(matches);
    }

}

public class MultiThreadingSearch {
    String path;
    String whatFind;

    ArrayListOfFiles textFiles;

    MultiThreadingSearch(String path, String whatFind) {
        this.path = path;
        this.whatFind = whatFind;
        this.textFiles = new ArrayListOfFiles();

        Thread pathFind = new Thread(new FolderScan(path, whatFind));
//      pathFind.start();

        if (!textFiles.isEmpty()) {
            @SuppressWarnings("unused")
            FileScan fileScan = new FileScan(textFiles.dequeue(), whatFind);
        }

    }

    // ask user about input
    public static void askUserPathAndWord() {

        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(System.in));
        String path;
        String whatFind;
        try {
            System.out.println("Please, enter a Path and Word"
                    + "(which you want to find):");
            System.out.println("Please enter a Path:");
            path = bufferedReader.readLine();
            System.out.println("Please enter a Word:");
            whatFind = bufferedReader.readLine();

            if (path != null && whatFind != null) {
                new MultiThreadingSearch(path, whatFind);
                System.out.println("Thank you!");
            } else {
                System.out.println("You did not enter anything");
            }

        } catch (IOException | RuntimeException e) {
            System.out.println("Wrong input!");
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        askUserPathAndWord();
    }
}

I got Exception in thread "main" java.lang.StackOverflowError from this code.
How able to solve this task?

Thanks,
Nazar.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-19T00:47:26+00:00Added an answer on June 19, 2026 at 12:47 am

    Check BlockingQueue it does exactly what you need. Thread can block until some other thread add new item to queue.
    As to how decompose you system. I’d do following:

    • Create class for searching txt files in path. It implements Runnable. You pass path and queue to it. And it searches path for txt files and adds them to the queu.
    • Create class for searching file content. It implements Runnable. You pass whatFind and queue to it and it takes new file from queue and checks it’s content.

    Something like:

    BlockingQueue<File> queue = new LinkedBlockingQueue<File>();
    String path = ...;
    String whatFind = ...;
    FolderScan folderScan = new FolderScan(path, queue);
    FileScan fileScan = new FileScan(whatFind, queue);
    
    Executor executor = Executors.newCachecThreadPool();
    executor.execute(folderScan);
    executor.execute(fileScan);
    

    If you want FileScan to wait until FolderScan adds something to the queue you can use take method:

    BlockingQueue<File> queue;
    File toProcess = queue.take(); // this line blocks current thread (FileScan) until someone adds new item to the queue.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to clean up various Word 'smart' characters in user input, including but
I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have an autohotkey script which looks up a word in a bilingual dictionary
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I need a function that will clean a strings' special characters. I do NOT
I have a bunch of posts stored in text files formatted in yaml/textile (from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.