Hello,
I need help here.
I found this magic Atomic Integer expression and read a little about this and, I think, I used that properly in line 52.
But problem is in Req(1) and rest Req(2,3,4,5) good.
But when I comment line 50,53 (if condition) then Req(1,2) is good and rest Req(3,4,5) fail.
And I am not sure that synchronized expression is needed (line 45).
package pl.codegym.task.task16.task1628;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
public class Solution {
public static volatile AtomicInteger readStringCount = new AtomicInteger(0);
public static volatile BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException {
// Wczytuje liczbę ciągów
int count = Integer.parseInt(reader.readLine());
// Inicjuje wątki
ReaderThread consoleReader1 = new ReaderThread();
ReaderThread consoleReader2 = new ReaderThread();
ReaderThread consoleReader3 = new ReaderThread();
consoleReader1.start();
consoleReader2.start();
consoleReader3.start();
while (count > readStringCount.get()) {
}
consoleReader1.interrupt();
consoleReader2.interrupt();
consoleReader3.interrupt();
System.out.println("#1:" + consoleReader1);
System.out.println("#2:" + consoleReader2);
System.out.println("#3:" + consoleReader3);
reader.close();
}
public static class ReaderThread extends Thread{
private List<String> result = new ArrayList<>();
String text;
public synchronized void run(){
//tutaj wpisz swój kod
try {
while(!isInterrupted()) {
text = reader.readLine();
if(!text.equals("")) {
result.add(text);
readStringCount.getAndIncrement();
}
}
}catch (IOException e) {
System.out.println(e);
}
}
@Override
public String toString() {
return result.toString();
}
}
}