I have 2 Thread : main and Thread2.
Main-->create Thread2, sleep for 3 second, exit.
Thread2--> readline from System.in and exit.
I want to wake up Thread2 if it is block in a readline(), i don't want to use timeout, and closing the main inputstream by generating an exception in Thread2 don't work .
The code :
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.InterruptedIOException;
public class Main {
public static void main(String[] args) throws InterruptedException {
InputStreamReader sc = new InputStreamReader(System.in);
Thread2 t = new Thread2(sc);
Thread tt = new Thread(t);
tt.start();
Thread.sleep(3000);
System.out.println("exit sleep");
tt.interrupt();
System.out.println("exit main");
}
}
class Thread2 implements Runnable {
InputStreamReader qst;
public Thread2(InputStreamReader sc) {
qst = sc;
}
public void run() {
BufferedReader buff = new BufferedReader(qst);
try {
System.out.println("read thread");
buff.readLine(); //Here is locked!!!!!!!!!!!!!!!!!!
} catch (InterruptedIOException e) {
System.out.println("exit thread");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Execution (println):
-read Thread
(after 3 second)
-exit sleep
-exit main
But Thread2 non stop--> it is block in a readline. Why?