- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个控制台应用程序,我想在其中的命令行上放置一个不确定的进度条,同时完成一些繁重的计算。目前我只是打印出一个“。”对于类似于以下内容的 while 循环中的每次迭代:
while (continueWork){
doLotsOfWork();
System.out.print('.');
}
这行得通,但我想知道是否有人有更好/更聪明的想法,因为如果循环中有很多迭代,这会变得有点烦人。
最佳答案
这是一个显示旋转进度条和传统样式的示例:
import java.io.*;
public class ConsoleProgressBar {
public static void main(String[] argv) throws Exception{
System.out.println("Rotating progress bar");
ProgressBarRotating pb1 = new ProgressBarRotating();
pb1.start();
int j = 0;
for (int x =0 ; x < 2000 ; x++){
// do some activities
FileWriter fw = new FileWriter("c:/temp/x.out", true);
fw.write(j++);
fw.close();
}
pb1.showProgress = false;
System.out.println("\nDone " + j);
System.out.println("Traditional progress bar");
ProgressBarTraditional pb2 = new ProgressBarTraditional();
pb2.start();
j = 0;
for (int x =0 ; x < 2000 ; x++){
// do some activities
FileWriter fw = new FileWriter("c:/temp/x.out", true);
fw.write(j++);
fw.close();
}
pb2.showProgress = false;
System.out.println("\nDone " + j);
}
}
class ProgressBarRotating extends Thread {
boolean showProgress = true;
public void run() {
String anim= "|/-\\";
int x = 0;
while (showProgress) {
System.out.print("\r Processing " + anim.charAt(x++ % anim.length()));
try { Thread.sleep(100); }
catch (Exception e) {};
}
}
}
class ProgressBarTraditional extends Thread {
boolean showProgress = true;
public void run() {
String anim = "=====================";
int x = 0;
while (showProgress) {
System.out.print("\r Processing "
+ anim.substring(0, x++ % anim.length())
+ " ");
try { Thread.sleep(100); }
catch (Exception e) {};
}
}
}
关于java - java命令行上的非确定性进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9302117/
我使用 Deflater 编写了一个备份程序SHA-1 用于存储文件和哈希值。我看到Java的Deflater使用zlib 。如果我显式设置 Deflater 的级别,无论平台和 JRE 版本如何,我
考虑以下算法: r = 2 while r >= 1: x = -1 + 2 * random.random() y = -1 + 2 * random.random() r
我正在编写一个持久保存到磁盘的映射类。我目前只允许 str键,但如果我可以使用更多类型会很好:希望最多可以是任何可散列的(即与内置 dict 相同的要求),但更合理的是我会接受字符串、unicode、
我有一个不使用随机化的脚本,当我运行它时会给出不同的答案。我希望每次运行脚本时答案都是一样的。该问题似乎只发生在某些(病态)输入数据上。 该代码段来自一种计算线性系统特定类型 Controller 的
这对我来说不是问题,没有它我也可以生活,但我只是好奇这是否可能以及如何实现。 今天我了解到,scrapy.Request 不会按照启动的顺序完成。 伪代码示例: class SomeSpider(sc
我正在运行这个 SVD来自 scipy 的求解器,代码如下: import numpy as np from scipy.sparse.linalg import svds features = np
我正在尝试使用确定性 Miller-Rabin 算法实现素数检查功能,但结果并不总是正确的:在检查前 1,000,000 个数字时,它只找到 78,495 而不是 78,498。 这是使用 [2, 7
我正在审查各种 Android 声音 API,我想知道我应该使用哪一个。我的目标是获得低延迟的音频,或者至少是关于播放延迟的确定性行为。 我们遇到了很多问题,Android 声音 API 似乎很垃圾,
过去,我处理过对时间要求严格的软件开发。这些应用程序的开发基本上是这样进行的:“让我们编写代码,测试延迟和抖动,并优化它们,直到它们在可接受的范围内。”我觉得这非常令人沮丧。这不是我所说的 适当的工程
给定: SQL Server 表名为 TEST_TABLE TEST_TABLE 中名为 TEST_FIELD 的列 VARCHAR(50) NOT NULL 第 1 行:10YR3/6 第 2 行:
我在 64 位 Windows PC 上使用 Microsoft Visual Studio Community 2015,版本 14.xxx。 程序读取一个文本文件,其中每一行都是桥牌(四名玩家,每
我需要在 PHP 中创建一个可证明公平(确定性和种子)加密安全 (CS) 随机数生成器。我们正在运行 PHP 5,而 PHP 7 现在并不是一个真正的选择。但是,我找到了 PHP 7 的新 CS 函数
我是一名优秀的程序员,十分优秀!