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 {
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 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;
}
}
这将等待用户输入5秒。如果用户不输入任何内容,则显示为空。
现在的问题是:
当我运行它时,它会等待5秒等待我的输入,但我没有输入任何内容,因此输出为空。然后在第二个输入中,输入“hi”。但它仍然会等待5秒(这不应该),在接受输入之后,它仍然显示为空。输出如下:
null
hi
null