可停止 Java 控制台进度条

Stoppable Java console progressbar

我正在寻找一种方法来停止以下栏的进度:

private static void cancellaaaaaaaaable() throws InterruptedException {
    System.out.println("Cancellaaaaaaable!");
    System.out.print(ANSI_RED);
    for(int i = 0; i < 79; i++){
        // Something that allows user input/interaction capable to stop the progressbar
        System.out.print("█");
        TimeUnit.MILLISECONDS.sleep(500);
    }
    System.out.print(ANSI_RESET);
}

当进度条启动时,用户应该有大约 40 秒的时间来决定停止和取消进度条。

有办法吗?任何键盘输入都很棒,除了那些粗暴地停止进程的键盘输入。

这里有一个解决方案,灵感来自 如何从标准输入非阻塞读取?

想法是检查System.in是否有可读的东西。

请注意,这仅在输入经过验证时有效(例如使用 Enter 键),但即使是空输入也有效(Enter 键直接按下),所以你可以添加一些 "Press Enter to cancel" 消息。

private static void cancellaaaaaaaaable() throws InterruptedException {
    System.out.println("Cancellaaaaaaable!");
    System.out.print(ANSI_RED);

    InputStreamReader inStream = new InputStreamReader(System.in);
    BufferedReader bufferedReader = new BufferedReader(inStream);
    for (int i = 0; i < 79; i++) {

        System.out.print("█");
        TimeUnit.MILLISECONDS.sleep(500);

        // Something that allows user input/interaction capable to stop the progressbar
        try {
            if (bufferedReader.ready()) {
                break;
            }
        } catch (IOException e) {

            e.printStackTrace();
        }
    }
    System.out.print(ANSI_RESET);
}

请注意,您可以使用 Java Curses Library 执行更高级的控制台操作。

定义一个可调用对象:

class MyCallable implements Callable<Boolean> {

    @Override
    public Boolean call() throws Exception {
        for (int i = 0; i <= 99; i++) {
            System.out.print("█");
            try {
                TimeUnit.MILLISECONDS.sleep(500);
            } catch (InterruptedException e) {
                e.getStackTrace();
                return false;
            }
        }
        return true;
    }
}

然后是 FutureTask 和 ExecutroService:

public static void main(String[] args) throws InterruptedException {
    System.out.println("Cancellaaaaaaable!");
    MyCallable callable1 = new MyCallable();
    FutureTask<Boolean> futureTask1 = new FutureTask<>(callable1);

    ExecutorService executor = Executors.newFixedThreadPool(1);
    executor.execute(futureTask1);
    TimeUnit.MILLISECONDS.sleep(500);
    futureTask1.cancel(true);
    System.out.println("\nDone");

}

并在需要时取消它:

futureTask1.cancel(true);