3

I'm having some problems with threads. I understand how they work, but since they all use the same method, how do I run different threads that do completely different things, but at the same time?

To me, it seems that they always use the same standard method which makes them do the same thing.

So, let's say I have a big .txt file where I want to go through each line and do something to the line. In this case, I would like to have each thread do 1/10th of the .txt file, but I don't understand how the threads can communicate with each other, and how they could organize so each thread does the right part?

Could anyone explain or help me with this? Would be very much appreciated!

2
  • 1
    This is a big question. Maybe you should consider doing some code to take a stab at it, and include that in this question as well Commented Mar 19, 2010 at 12:27
  • You also have to consider if it makes sense to access the same with multiple threads concurrently. If the work they do for each line is very small, the I/O cost for reading the file is relatively large and multi-threading will not help. If you do something bigger (such as downloading a file from a URL in that line), it could be good idea. Commented Mar 19, 2010 at 12:36

3 Answers 3

8

You can extend java.lang.Thread (or better - implement java.lang.Runnable) and pass arguments to the constructor of the new object. For the text file example:

public FileReader implements Runnable {
    private int startLine;
    private int endLine;
    public FileReader(int startLine, int endLine) {
       // assign the params to the fields
    }

    public void run() {
       // use the params to read the appropriate lines
    }
}

and then you can:

new Thread(new FileReader(1, 10)).start();
new Thread(new FileReader(11, 20)).start();
new Thread(new FileReader(21, 30)).start();
Sign up to request clarification or add additional context in comments.

2 Comments

Bozho, that helped me A LOT. Thanks so much. I'll go experiment a bit and will be back later with feedback. If anyone else has anything to note, please do so. Thanks again!
@CrisCarter I doesn't look like you know this but if any answer helps you a lot (such as this one), and/or actually answers your original question you should accept by pressing the tick underneath the up and down vote arrows on the answer that you would like to accept. That way you will keep your reputation up and others will be more inclined to help you!
5

Nowadays you probably should have look at java.util.Concurrent instead of tinkering with primitive threads (although they can of course be used where appropriate). Threading is quite a big subject, but by using well-defined idioms from the Concurrent package it can become a bit more bearable.

Comments

-3

As far as i know, threads are independent pieces running in parallel and there is always a master thread (usually Main thread) that controls parallel running worker threads. There is no need for threads to communicate with each other since that task is taken care by the Master thread.

In your case, you can have the Master thread to send arguments to the worker threads stating line numbers - 0 to 10 for thread1, 11 to 20 for thread2 etc. The worker threads take these numbers as input and process the file accordingly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.