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 7665239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:30:46+00:00 2026-05-31T14:30:46+00:00

I have two java classes called Reader and Worker. Reader reads strings from a

  • 0

I have two java classes called Reader and Worker. Reader reads strings from a text file and passes these strings to Workers which sort these strings into a tree. So I create a number of Worker-threads and 1 Reader-thread:

        try {      

        /* Create and run worker processes. */
        workers = new TrieWorker[numberOfWorkers];
        for(int i = 0; i < numberOfWorkers; i++)
        {
            workers[i] = new TrieWorker(this);
            new Thread(workers[i]).run();
        }

        /* Create and run reader process. */
        reader  = new TrieReader(this, filename);                        
        new Thread(reader).run();

    } catch(Exception e) {
        e.printStackTrace();
    }

When my Reader reads a string from a text file inside run()-method, it passes the string to one of the workers by calling

workers[i].add(string);

Workers are basically doing nothing until this method has been called. So I’m wondering, does Reader continue reading from a text file immediately after it passes this parameter to one of the Workers? Or does it return from this method only after Worker is done with the string? In other words, when one Thread passes parameters to another one, do they both continue to do their own thing unless Thread passing parameters wants some kind of an answer back?

Hope you guys know what I mean, really hard to explain.

EDIT: Thanks for the good answers! Here’s code of Reader class:

private static int numberOfNextWorker = 0;

    public void run() 
{
    System.out.println("Reader run.");

    try {
        System.out.println("Reading " + filename);
        read();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

public void read() throws Exception
{
    File file = new File(filename);
    Scanner reader = new Scanner(file);

    if(file.canRead() == false)
        throw new Exception("file " + filename + " cannot be read.");

    long totalLength = file.length();

    while(reader.hasNextLine())
    {

            String text = reader.nextLine();          
            numberOfLines++;

            /* Passes the work on to workers. */
            if(numberOfNextWorker == (numberOfWorkers - 1))
                 numberOfNextWorker = 0;
            else
                 numberOfNextWorker++;

            workers[numberOfNextWorker].add(text); 
        }
}

And here Worker:

    public void run() 
{
    System.out.println("Worker run.");
}

void add(String text) 
{
    numberOfStrings++;
    char[] chars = text.toCharArray();

    int i = 0;
    Node node = new Node();

    while (chars.length > i) {
        node.value = chars[i];
        node = node.children;
        i++;
    }
}

It’s not doing anything wise yet 🙂 I think I understood what you said. I have read this book about concurrency a lot and done some exercises on paper but haven’t tried to code anything that much.

  • 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-05-31T14:30:48+00:00Added an answer on May 31, 2026 at 2:30 pm

    You need a third class called resource. Your threads will communicate through the shared resource. Think producer-consumer Your workers will add to sortedReult.

    public class Resource{
      Queue<String> semaphore = new LinkedList<String>();
      Queue<String> sortedResult = new LinkedList<String>();
    
      public synchronized addStrings(List<String> words){//for reader
        semaphore.addAll(words);
        notify();
      }//
    
      public synchronized String getString(){//for workers
        while(semaphore.isEmpty())
           try{ wait();}
           catch(InterruptedException e){}
        return semaphore.remove();
      }
    }
    

    by the way, that you are calling run() shows you kind of understand the logic. But in Java, you must call start() and let start() in turn call run() for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two Java classes: B, which extends another class A, as follows :
I'm making a shopping list mobile application (Java ME) and i have two classes;
How do I do forward declarations in Java? I have two classes, each needs
I have two Java.io.File objects file1 and file2. I want to copy the contents
I have two strings in a java program, which I want to mix in
I have two java classes. One is NewFrame.java where the forms(buttons, textfields) are located.
At this moment, I have two classes: UserHibernateDao and TicketHibernateDao : import java.util.List; import
I have two Java-Classes as follows: public class MyClass { ... } public class
I have written two simple Java classes (one of them containing main(), and the
Firstly, I am new to both android and Java. I have two classes, my

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.