循环内输入的时间限制

Time limit for an input inside a loop

我想实现一个用户需要在固定时间限制内输入的功能。我已经完成了这个答案。

Time limit for an input

这个例子成功了一次。当用户未固定输入时,程序将终止。但我试图在一个循环中多次使用它。我每次都需要检查用户输入的内容。请参阅我的 InputTimer Class,根据 post 答案修改。

InputTimer.java

public class InputTimer {
    private String str = "";
    public String responseString = "";
    private BufferedReader in;
    TimerTask task = new TimerTask() {
        public void run() {
            if (str.equals("")) {
                responseString = "";
            }
        }
    };

    public void getInput() throws Exception {
        Timer timer = new Timer();
        timer.schedule(task, 5 * 1000);

        in = new BufferedReader(new InputStreamReader(System.in));
        str = in.readLine();
        timer.cancel();
        responseString = str;
    }
}

我已经尝试在循环中将其实现到另一个 class:

for(int i=0; i<5; i++){
    System.out.println("Enter res in 5 sec: ");
    InputTimer inputTimer = new InputTimer();
    try {
        inputTimer.getInput();
    } catch (Exception e) {
        e.printStackTrace();
    }
    String responseString = inputTimer.responseString;
    if(responseString.equals("res")){
        //User typed res
    } else {
        //
    }
}

问题: 根据我的逻辑,如果用户在 5 秒内输入 res,那么 responseString 值将是 res,否则它将是空值。 5 秒后此处仍在等待用户输入。

我的要求: 如果用户在 5 秒内输入 res,那么它将用于 //User typed res 个任务并进行下一次迭代。如果用户在 5 秒内没有输入任何内容,则用户无法输入(用户输入选项将消失),responseString 值将变为空,否则将执行块,然后再次进行下一次迭代。

请帮我找出这个场景的解决方案。

这是另一个尝试使用 Joe C 的解决方案。它只工作一次。检查代码:

public class test {
    private static BlockingQueue<String> lines = new LinkedBlockingQueue<>();
    public static String inputTimer() throws InterruptedException {

        return lines.poll(5, TimeUnit.SECONDS);
    }

    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            String tmp = "";
            try {
                System.out.print("Iteration " + i + ": Enter any in 5 sec: ");
                Thread t = new Thread(() -> {
                Scanner s = new Scanner(System.in);
                while (true) {
                    lines.add(s.nextLine());
                }
            });
            t.setDaemon(true); 
            t.start();
                tmp = inputTimer();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("You entered: " + tmp);
        }
    }
}

Output Log:

Iteration 0: Enter any in 5 sec: xx
You entered: xx
Iteration 1: Enter any in 5 sec: xx
You entered: null
Iteration 2: Enter any in 5 sec: xx
You entered: null
Iteration 3: Enter any in 5 sec: xx
You entered: xx
Iteration 4: Enter any in 5 sec: You entered: xx

这可以通过基于事件的模型来实现。您将需要两个线程(其中一个可以是主线程)。

第一个线程将接受来自 Scanner 的输入并将其添加到事件队列(在我们的例子中,"event" 只是输入的字符串):

private BlockingQueue<String> lines = new LinkedBlockingQueue<>();

在您的 main 方法中启动所述线程:

Thread t = new Thread(() -> {
    Scanner s = new Scanner(System.in);
    while (true) {
        lines.add(s.nextLine());
    }
});
t.setDaemon(true); // so that this thread doesn't hang at the end of your program
t.start();

然后,当你想获取输入时,你可以从队列中获取,超时:

return lines.poll(5, TimeUnit.SECONDS);