1) How to print the result Array as it has to be non-static. Otherwise it keeps adding the same lines twice
2) How to show that the reading is sequential and not parallel?
package com.codegym.task.task16.task1629;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class Solution {
public static volatile BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws InterruptedException {
Read3Strings t1 = new Read3Strings();
Read3Strings t2 = new Read3Strings();
//write your code here
//The main method should call the created threads' start methods.
t1.start();
t2.start();
//The main method should call the created threads' join methods.
t1.join();
t2.join();
t1.printResult();
t2.printResult();
}
//write your code here
//In the Solution class, declare the public static Read3Strings class.
//The Read3Strings class should inherit the Thread class.
public static class Read3Strings extends Thread {
//The Read3Strings class's run method must read three lines.
ArrayList<String> result = new ArrayList<>();
public void run() {
try {
for (int i = 0; i < 3; i++) {
result.add(reader.readLine());
return result;
}
} catch (IOException e) {
e.printStackTrace();
}
}
//The Read3Strings class must have a public printResult method.
public static void printResult() {
//The printResult method should display the 3 read lines, separating them with spaces.
//The program's output should indicate that the threads read the lines sequentially, not in parallel.
for (int i = 0; i < result.size(); i++) {
System.out.print(result.get(i) + " ");
}
}
}
}