简体   繁体   中英

Java: Thread passing parameters to another thread

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.

Note that you are not starting a new thread at any point, but only call the run() method on the same thread.

You need to call to start() to spawn a new thread, for example:

new Thread(workers[i]).start();

instead of

new Thread(workers[i]).run();

How specifically the threads will continue working depends on your implementation.

The call to Worker.add is blocking, so it waits until add() is done. This is all running in Thread 1. If Worker.add should merely adds the work to a list of work, and in the run() method handle its work (executed in its own thread).

This is a classical Consumer/Producer problem. (Blocking) Queues might help you.

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.

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