Question

Develop a java program using multithreading in which each thread reads data from two separate input...

Develop a java program using multithreading in which each thread reads data from two separate input text files and display each line of data from each file on the Console alternatively such that one line from the first input file is printed and then one line from the other input file is printed etc.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

Here is the code to read the 2 files in 2 separate threads and print each line from the file. You cannot have control on the threads during execution. You will always see different O/Ps on the console, as the execution part is handled by the JVM. I hope the below code helps, do comment if you have any questions.

If /usr/lib/jvm/java-8-oracle/bin/java ... The thread - 1The thread -2 David Webb Box 34 Rural route 2 Nixa MO 65714 (417)555

MultiThreadFileRead.java

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class MultiThreadFileRead {

    public static void main(String[] args) throws InterruptedException {

        Thread thread1 = new Thread(new Runnable() { // creating thread 1
            public void run() {
                System.out.print("The thread - 1");
                try {
                    Scanner sc1 = new Scanner(new File("src/main/java/copy_customers_and_contacts.txt"));
                    while(sc1.hasNextLine()){

                        System.out.println(sc1.nextLine() + " : thread 1");
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
        });

        Thread thread2 = new Thread(new Runnable() { // creating thread - 2
            @Override
            public void run() {
                System.out.println("The thread -2");
                try {
                    Scanner sc1 = new Scanner(new File("src/main/java/customers_and_contacts.txt"));
                    while(sc1.hasNextLine()){
                        System.out.println(sc1.nextLine() + " : thread 2");
                    }
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
        });
        
        thread1.start();
        thread2.start();

    }

}

Thank you, Good Luck!!!
Add a comment
Know the answer?
Add Answer to:
Develop a java program using multithreading in which each thread reads data from two separate input...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA Write a program that prompts the user to enter a file name, then opens the...

    JAVA Write a program that prompts the user to enter a file name, then opens the file in text mode and reads it. The input files are assumed to be in CSV format. The input files contain a list of integers on each line separated by commas. The program should read each line, sort the numbers and print the comma separated list of integers on the console. Each sorted list of integers from the same line should be printed together...

  • C++ please Write a program that reads the following sentences from a file: I am Sam...

    C++ please Write a program that reads the following sentences from a file: I am Sam Sam I am That Sam I am That Sam I am I do not like that Sam I am Do you like green eggs and ham I do not like them But I do like spam! You will first have to create the text file containing the input, separate from your program. Your program should produce two output files: (i) (ii) one with the...

  • Write a program in Java that reads the file and does two things: Write to an...

    Write a program in Java that reads the file and does two things: Write to an output file called dataSwitch.txt the same data from the input file, but switch the order of the data entries on each line. For example, if the first line of the input file is “90001 57110”, the first line of the output file would be “57110 90001”. Additionally, print to the screen the number of lines in the file with a label

  • ( IN JAVA): Write a program that reads each line in a file, and writes them...

    ( IN JAVA): Write a program that reads each line in a file, and writes them out in reverse order into another file. This program should ask the user for the name of an input file, and the name of the output file. If the input file contains: Mary had a little lamb Its fleece was white as snow And everywhere that Mary went The lamb was sure to go Then the output file should end up containing: The lamb...

  • Using java Create a simple queue class and a simple stack class. The queue and stack...

    Using java Create a simple queue class and a simple stack class. The queue and stack should be implemented as a linked list. Create three functions that utilize these data structures Write a function that opens a text file and reads its contents into a stack of characters. The program should then pop the characters from the stack and save them in a second text file. The order of the characters saved in the second file should be the reverse...

  • This Java program reads an integer from a keyboard and prints it out with other comments....

    This Java program reads an integer from a keyboard and prints it out with other comments. Modify the comments at the top of the program to reflect your personal information. Submit Assignment1.java for Assignment #1 using Gradescope->Assignemnt1 on canvas.asu.edu site. You will see that the program has a problem with our submission. Your program is tested with 4 testcases (4 sets of input and output files). In order to pass all test cases, your program needs to produce the same...

  • Please help with Java Programming project! Project outcomes: Develop a Java program that: .reads input from...

    Please help with Java Programming project! Project outcomes: Develop a Java program that: .reads input from the keyboard using a Scanner object and its methods uses iteration (while, do while, or for statements uses String comparison methods. follows standard acceptable programming practices. .handles integer overflow errors Prep Readings: Absolute Java Chapter 1 3 and Security Injection Labs (Integer Error) Project Requirements: Write a Java program that plays a mathematical game called "Taking Stones" which is based on the Chinese game...

  • I need to write a C++ program using DEV C++ that reads and prints a joke and its punch line from...

    I need to write a C++ program using DEV C++ that reads and prints a joke and its punch line from two different files. The first file contains a joke, but not its punchline. The second file has the punch line as its last line, preceded by “ garbage.” The main function of the program should open the two files and then call twofunctions, passing each one the file it needs. The first function should read and display each line...

  • FOR JAVA Write a program that takes two command line arguments: an input file and an...

    FOR JAVA Write a program that takes two command line arguments: an input file and an output file. The program should read the input file and replace the last letter of each word with a * character and write the result to the output file. The program should maintain the input file's line separators. The program should catch all possible checked exceptions and display an informative message. Notes: This program can be written in a single main method Remember that...

  • IN JAVA. Write a program that reads a file (provided as attachment to this assignment) and...

    IN JAVA. Write a program that reads a file (provided as attachment to this assignment) and writes the file to a different file with line numbers inserted at the beginning of each line. For example if file input is: This is a test File output should be 1. This is a test ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ BELOW FROM NOTEPAD FILE. This file contains lines of text to determine if you can properly read and write from a file.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT