1

My program is as below:

import java.io.*;
class MyThread implements Runnable{
    int st,en;
    LineNumberReader lmr1;
    BufferedWriter bw;
        MyThread(int s,int e){
        st=s;
        en=e;
    } 
    public void run(){

        try{

                bw=new BufferedWriter(new FileWriter("EmpWritten.txt"));
                            lmr1=new LineNumberReader(new FileReader("out3.txt"));
                String s=Thread.currentThread().getName();
                            lmr1.skip(st);
                while(st++<en){
                        bw.write(s+"\t"+lmr1.readLine());
                        bw.newLine();

                }
            lmr1.close();
            bw.close();
        }
        catch(Exception e){}

}
    public static void main(String args[])throws Exception{
        LineNumberReader lmr=new LineNumberReader(new FileReader("out3.txt"));
        lmr.skip(Long.MAX_VALUE);
        int num=lmr.getLineNumber();
        int num1=num/3,num2=2*num1;

        MyThread mt1=new MyThread(0,num1);
        MyThread mt2=new MyThread(num1+1,num2);
        MyThread mt3=new MyThread(num2+1,num);

        Thread t1=new Thread(mt1);
        Thread t2=new Thread(mt2);
        Thread t3=new Thread(mt3);

        t1.start();
        t2.start();
        t3.start(); 


        lmr.close();
    }
}

Input:

Commit ID Name Salary Department Number
Commit ID1 Name1 25100 Dept1
Commit ID2 Name2 25200 Dept2
Commit ID3 Name3 25300 Dept3
Commit ID4 Name4 25400 Dept4
Commit ID5 Name5 25500 Dept5
Commit ID6 Name6 25600 Dept6
Commit ID7 Name7 25700 Dept7
Commit ID8 Name8 25800 Dept8
Commit ID9 Name9 25900 Dept9
Commit ID10 Name10 26000 Dept10

output:

Thread-2 ID Name Salary Department Number
Thread-2 Commit ID1 Name1 25100 Dept1
Thread-2 Commit ID2 Name2 25200 Dept2
Thread-2 Commit ID3 Name3 25300 Dept3

Why don't other threads execute?What modifications need to be done to get the desired output?Efficient implementations of the program is also welcome.

Leo18
  • 69
  • 1
  • 6
  • Why do you think they don't execute? Each thread writes to the same file, so all you see is the output of the last thread to write, which overwrites the output of the previous ones. You're also ignoring exceptions, so if one thread fails to do anything, you can't be aware of it. Don't ever do that. – JB Nizet Feb 10 '15 at 11:55
  • That's a backwards approach to performance. If you want to leverage all your cores while using sequential file reading, then use one thread dedicated to reading and dispatch work to several worker threads. – Marko Topolnik Feb 10 '15 at 11:56
  • @MarkoTopolnik Can you tell how to do that? – Leo18 Feb 10 '15 at 12:37
  • Read lines in chunks of such size that their processing takes at least 10 milliseconds; for each chunk submit a task to an ExecutorService. You'll still need to work out how to collect all the results in order. – Marko Topolnik Feb 10 '15 at 12:41
  • 2
    Or use the Streams APi to get most of the parallelization concerns solved out of the box. Check out https://www.airpair.com/java/posts/parallel-processing-of-io-based-data-with-java-streams and http://stackoverflow.com/questions/25408350/is-there-a-good-way-to-extract-chunks-of-data-from-a-java-8-stream – Marko Topolnik Feb 10 '15 at 12:46

1 Answers1

0

Three threads worked. You can see it after adding some logs in your run method. I am not sure what you want exactly. But I think you need producer-consumer design pattern.

Ricky
  • 1
  • 1