1

I am trying this program in java but I am not getting any output when I put everything in the run() method

Main.java:

public class Main {

    static int line;
    static boolean ret = true;
    static BufferedReader br;

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {

            File f = new File("tere.dat");
            // boolean ret = f.createNewFile() ;
            br = new BufferedReader(new FileReader(f));

            new Test(br.readLine());
            new Test(br.readLine());
    }

}    

Test.java:

public class Test extends Thread {

    private String input;
    static int thread_count = 0;

    public Test(String l)
    {
         input = l;
    }

    public void run()
    {
        System.out.println("Checking from other class  : This was printed from file :>>");
        System.out.println(input);
        String upper = input.toUpperCase();
        System.out.println("");
        System.out.println("The String in all UpperCase :" + upper);
    }
}

What I want to do is that I want to read lines from a file using two threads and then display whatever I get . I am new to Java

EDIT :

I was not using the start() method. Though even after using start() It reads only 2 lines from the file and stops. What could be the problem ?

4 Answers 4

4

You have to start() your Threads.

Also, i would suggest reading a good tutorial on Threads and concurrency in Java before proceeding, as it's a complex subject.

Sign up to request clarification or add additional context in comments.

Comments

1

You need to start your thread with start() I suggest you not extend Thread but instead implement Runnable.

BTW: Unless you type impossibly fast, you won't see the difference in using threads. It will take about 0.1 milli-seconds to start the thread and finish it so unless you type much faster than that, it will make no difference.

2 Comments

I know that. But I was just trying to understand the Thread Model :) thanks for the reply :)
@DeepankarBajpeyi I suggest you input the two lines before start the threads, or just use hard coded Strings like "Hello" and "World".
1

You need to start threads:

(new Test(br.readLine())).start();

And also, you have to add some join to wait threads to finish because your main thread will finish execution before created threads.

2 Comments

Is join really necessary? main thread is done after the two threads are created, so it doesnt need to wait for them to do whatever they do.
It might finish before the threads, that is what I think partlov is trying to suggest
1

You have to start them:

        (new Test(br.readLine())).start();
        (new Test(br.readLine())).start();

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.