import java.io.*;
import java.util.concurrent.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.Callable;
class _TimeOut_ extends PrintIn_Delays {
public static void main(String[] args)
throws InterruptedException {
TimeWait Timeout = new TimeWait();
String input = Timeout.readLine();
String input2 = Timeout.readLine();
}
}
class Reader implements Callable<String> {
public String call() throws IOException {
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
String input;
do {
input = br.readLine();
}while ("".equals(input));
return input;
}
}
class TimeWait extends _TimeOut_ {
public String readLine() throws InterruptedException {
ExecutorService ex = Executors.newSingleThreadExecutor();
String input = null;
try {
try {
Future<String> result = ex.submit(
new Reader());
input = result.get(5, TimeUnit.SECONDS);
} catch (ExecutionException e) {
e.getCause().printStackTrace();
} catch (TimeoutException e){}
} finally {
ex.shutdownNow();
}
System.out.println(" "+input);
return input;
}
}
This will wait for 5 seconds for user input. If user don't enter anything, it displays null. Now the problem is : When I run it, it waits for 5 seconds for my input but I don't enter anything and so output is null. Then in the second input, I enter 'hi'. But it still waits for 5 seconds ( which it shouldn't) and after taking the input, it still displays null. Here is the output :
null
hi
null