java 控制台进度条(基于守护进程)

有时候我们有这样一种需求,我们需要进行长时间的IO读写。但是又是直接调用封装的方法。没办法打印日志,

我们希望可以在控制打印当前IO的读写状态。在不考虑读写性能的前提下,我的思路是:

  • + new一个打印特殊字符的守护线程出来,间隔时间打印字符串,当IO读写线程结束时,打印字符串线程也结束。
 /**
     * <per>
     * <p>控制台进度条</p>
     * <per/>
     *
     * @param
     * @return void
     * @throws
     * @Description
     * @author Liruilong
     * @Date 2020年08月19日  15:08:12
     **/
    public static void progress() {
        Thread thread = new Thread(() -> {
            StringBuilder tu = new StringBuilder("▧");
            while (true) {
                tu.append("▧");
                System.out.print("
本地构建" + "Zip中:	" + tu + "	");
                try {
                    TimeUnit.MILLISECONDS.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        thread.setDaemon(true);
        thread.start();
    }
原文地址:https://www.cnblogs.com/liruilong/p/13533011.html