如何阻止Java Scanner接受输入 [英] How to stop Java Scanner from accepting input

查看:260
本文介绍了如何阻止Java Scanner接受输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我正在开发一个可以从命令行运行的测验计划,测验有时间限制。我想做的是,即使用户正在回答问题,也要在用户的时间到了之后立即停止测验。我正在使用Java的扫描仪来获取用户的输入,所以我想基本上告诉Scanner对象即使它正在接受输入的中间也要终止。

So, I am working on a quiz taking program that works from the command line and the quizzes have time limits. What I want to do, is to stop the quiz right when the user's time is up even if they're in the middle of answering a question. I am using Java's Scanner to get the user's input, so I want to essentially tell the Scanner object to terminate even if it's in the middle of accepting input.

现在,我知道我可以追溯惩罚用户在事后经历一段时间,但我只是希望在超过时间限制后终止测验。有没有办法用多线程来做到这一点?

Now, I know that I can retroactively punish a user for going over time after the fact, but I simply want the quiz to terminate once the time limit has been exceeded. Is there any way to do this with multithreading for example?

推荐答案

Java Scanner正在使用阻止操作。不可能阻止它。甚至不使用 Thread.interrupt();

A Java Scanner is using blocking operations. It is not possible to stop it. Not even using Thread.interrupt();

但是你可以使用 BufferedLineReader读取并能够停止该线程。这不是一个简洁的解决方案,因为它涉及暂停暂停(否则它将使用100%CPU),但它确实有效。

You can however read using a BufferedLineReader and be able to stop the thread. It's not a neat solution, as it involves pausing for short moments (otherwise it would use 100 % CPU), but it does work.

public static class ConsoleInputReadTask {
    private final AtomicBoolean stop = new AtomicBoolean();

    public void stop() {
        stop.set(true);
    }

    public String requestInput() throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("ConsoleInputReadTask run() called.");
        String input;
        do {
            System.out.println("Please type something: ");
            try {
                // wait until we have data to complete a readLine()
                while (!br.ready() && !stop.get()) {
                    Thread.sleep(200);
                }
                input = br.readLine();
            } catch (InterruptedException e) {
                System.out.println("ConsoleInputReadTask() cancelled");
                return null;
            }
        } while ("".equals(input));
        System.out.println("Thank You for providing input!");
        return input;
    }
}

public static void main(String[] args) {
    final Thread scannerThread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                String string = new ConsoleInputReadTask().requestInput();
                System.out.println("Input: " + string);
            }
            catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    });
    scannerThread.start();

    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            scannerThread.interrupt();
        }
    }).start();
 }

这篇关于如何阻止Java Scanner接受输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆