I am trying to develop a java program containing two threads that reads two files simultaneously.

In a program, first thread reads data from file “Personal_Record.txt” and second thread reads the data from file “Academic_Record.txt”.

When one thread reads a line from one file then it should allow another thread to read a line from another file.
After reading data from each file, program must write the output on console (Output screen) such that one line from first input file is printed and then one line from another input file is printed and so on.

Here are sample files
First File Name: "Personal_Record.txt"

STD01 Saqib 18
STD02 Kashif 17
STD03 Memoona 19
STD04 Madiha 18
STD05 Sidra 18
STD06 Hina 17
STD07 Shahid 19
STD08 Huma 18
STD09 Saeeda 17
STD10 Anjum 18

Second File Name: "Academic_Record.txt"

BCS 3.4 
BCS 3.5
MCS 2.6
BIT 3.0
BBA 2.7
BCS 3.0
MIT 3.2
BIT 3.4
BBA 3.2
BCS 2.9

Sample Output

STD01 Saqib 18
BCS 3.4
STD02 Kashif 17
BCS 3.5
STD03 Memoona 19
MCS 2.6
STD04 Madiha 18
BIT 3.0
STD05 Sidra 18
BBA 2.7
STD06 Hina 17
MIT 3.2
STD07 Shahid 19
BCS 3.0
STD08 Huma 18
BIT 3.4
STD09 Saeeda 17
BBA 3.2
STD10 Anjum 18
BCS 2.9
import java.io.*;

public class ReadingFileMT implements Runnable{

    //attribute used for name of file
    String fileName;

    ReadingFileMT(String fn){
        this.fileName = fn;
    }

    // overriding run method
    // this method contains the code for file reading
    public void run (){
        try{
            // connecting FileReader with attribute fileName

            FileReader fr = new FileReader(fileName);
            BufferedReader br = new BufferedReader(fr);

            String line = "";

            // reading line by line data from file
            // and displaying it on console
            line = br.readLine();

            while (line!=null){          
                System.out.println(line);
                Thread.sleep(1000);
                line = br.readLine();
            }

            fr.close();
            br.close();
        }
        catch (Exception e){
            System.out.println(e);
        }
    } // end run() method
}


public class ThreadFile {

    public static void main(String[] args) {

        //Creating ReadFileMT object passing file names to them;
        ReadingFileMT personalRecord = new ReadingFileMT("Personal_Record.txt");
        ReadingFileMT academicRecord = new ReadingFileMT("Academic_Record.txt");

        Thread firstFile = new Thread(personalRecord);
        Thread secondFile = new Thread(academicRecord);

        //Starting Threads
        firstFile.start();
                        //Thread.State.WAITING.wait();
        secondFile.start();

        try {
            //while(firstFile.isAlive()){
                firstFile.join(1000);
       //}
          
        }catch (Exception ex) {
            System.out.println(ex);
        }
    }
}

But I not produce same Output as I mention. Please help me in this regard to solve this problem

Hi....
I have tried your code on my PC. Its works well. output is :

STD01 Saqib 18
BCS 3.4 
STD02 Kashif 17
BCS 3.5
STD03 Memoona 19
MCS 2.6
STD04 Madiha 18
BIT 3.0
STD05 Sidra 18
BBA 2.7
STD06 Hina 17
BCS 3.0
STD07 Shahid 19
MIT 3.2
STD08 Huma 18
BIT 3.4
STD09 Saeeda 17
BBA 3.2
STD10 Anjum 18
BCS 2.9

So now the problem is not your code. So chill.

Thread sequence depends on many factors as System clock, processor type, OS, OS's scheduling algos. So try using some other concept instead of sleep.

Every time when I execute this program its change its output.

I think the previous poster was lucky to get the correct o/p (single processor?). Without proper synchronization the results will vary. Like I said: wait/notify.

So now the problem is not your code. So chill.

Not necessarily. There might not be a problem as such in his program but that does not mean the o/p would be as he wants it. It depends on the how the threads are schedule and hence, as mentioned, the same compiled program may generate various output sequences. I feel the advice of JC is the most valued here.

@hardik : You might have been lucky to get a sequence where the threads have been exactly alternating the execution cycle.

@OP : Follow what JamesC mentions about the wait/notify, it is the std procedure.

Not necessarily. There might not be a problem as such in his program but that does not mean the o/p would be as he wants it. It depends on the how the threads are schedule and hence, as mentioned, the same compiled program may generate various output sequences. I feel the advice of JC is the most valued here.

@hardik : You might have been lucky to get a sequence where the threads have been exactly alternating the execution cycle.

@OP : Follow what JamesC mentions about the wait/notify, it is the std procedure.

I have mentioned in my previous post that sleep doesn't works fine in all case.

I have mentioned in my previous post that sleep doesn't works fine in all case.

You might be surprised to know that sleep is not the only one that causes the problem, seldom actually. Any kind of blocking I/O is the main cause why a thread could be made to relinquish control of the CPU. And even if you remove the sleep command altogether you certainly have I/O there.

You might be surprised to know that sleep is not the only one that causes the problem, seldom actually. Any kind of blocking I/O is the main cause why a thread could be made to relinquish control of the CPU. And even if you remove the sleep command altogether you certainly have I/O there.

Darling read the posts carefully. :D

Darling read the posts carefully.

Firstly check your langauge here, this is not some cheap internet chat that you are into. Read the forum rules regarding chat speak, because I am sure you haven't cared.

And towards the answer to your post. I guess you are mentioning this :

Thread sequence depends on many factors as System clock, processor type, OS, OS's scheduling algos. So try using some other concept instead of sleep.

I don't find anything about I/O, which is the prime cause of concern here. You are mentioning a whole lot of other things here but not the one which would help the OP in figuring out the problem.

And this is the proof of that, which is what he says after your post:

Every time when I execute this program its change its output.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.