- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在逐行解析大文件,文件大小可能从几千行到数百万行。当文件很小(例如,只有几万行)时,所有组件(下图中红色箭头所示的组件)都会立即逐行更新,从而真实地指示进程的位置。当文件较大时,更新会有很大的延迟,可能需要长达30秒的时间指标才能反射(reflect)进度!我可以手动调整它的大小以刷新显示。
逐行流过文件时调用的方法是 updateProgress()
。其中,progressPrevVal
从 0 到 1(每个增量为 1.0/2,675,150
),并且 lineNum
如果我们使用图像中的示例。
完整代码如下:
import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
public class DxProgressIndicator extends Application {
private static ProgressBar pb;
private static ProgressIndicator pi;
private static TextField tfNumbers;
private static Label lblLineNumVal;
private static Label lblAllLines;
private static double progressIncrement;
private static double progressPrevVal;
private static int lineNum;
private static Label lblFile;
@Override
public void start(Stage stage) {
Group root = new Group();
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setTitle("Dexter Parser");
lblFile = new Label();
final HBox hbFile = new HBox();
hbFile.setAlignment(Pos.CENTER_LEFT);
Label lblCurrentFile = new Label("Current file: ");
lblCurrentFile.setMinWidth(90);
lblCurrentFile.setFont(Font.font(null, FontWeight.BOLD, 13));
hbFile.getChildren().addAll(lblCurrentFile, lblFile);
pb = new ProgressBar(0);
pb.setMinWidth(450);
pi = new ProgressIndicator(0);
final HBox hbPis = new HBox(10);
hbPis.setAlignment(Pos.CENTER_LEFT);
hbPis.getChildren().addAll(pb, pi);
lblLineNumVal = new Label();
lblLineNumVal.setMaxWidth(200);
Label slash = new Label(" / ");
lblAllLines = new Label();
final HBox hbLines = new HBox();
hbLines.setAlignment(Pos.CENTER_LEFT);
Label lblLineNum = new Label(" Currently parsing Line # : ");
lblLineNum.setFont(Font.font(null, FontWeight.BOLD, 12));
hbLines.getChildren().addAll(lblLineNum, lblLineNumVal, slash, lblAllLines);
tfNumbers = new TextField();
tfNumbers.setEditable(false);
tfNumbers.setMaxWidth(100);
final HBox hbTrxValues = new HBox();
hbTrxValues.setAlignment(Pos.CENTER_LEFT);
Label lblNumbers = new Label("(transaction, step, record) = ");
lblNumbers.setFont(Font.font(null, FontWeight.BOLD, 12));
hbTrxValues.getChildren().addAll(lblNumbers, tfNumbers);
final VBox vb = new VBox(10);
vb.getChildren().addAll(hbFile, hbPis, hbLines, hbTrxValues);
vb.setPadding(new Insets(10));
scene.setRoot(vb);
stage.show();
}
public static void updateProgress() {
Platform.runLater(() -> {
progressPrevVal += progressIncrement;
pb.setProgress(progressPrevVal);
pi.setProgress(progressPrevVal);
lblLineNumVal.setText(Integer.toString(lineNum++));
});
}
public static void setFileMetadata(String str) {
Platform.runLater(() -> {
lblAllLines.setText(str);
progressIncrement = 1 / Double.valueOf(str);
progressPrevVal = 0d;
lineNum = 1;
});
}
public static void main(String[] args) {
launch(args);
}
}
从客户端的 main 方法中,一旦我在自己的线程中启动 GUI,如下所示:
Runnable task = () -> {
DxProgressIndicator.main(null);
};
Thread thread = new Thread(task);
thread.start();
然后我可以浏览该文件,逐行流式传输,如下所示:
try (Stream<String> stream = Files.lines(file)) {
stream.forEach(line -> {
DxProgressIndicator.updateProgress();
// a bunch of stuff to do with the line
});
} catch (IOException e) {
}
那么,我错过了什么?在处理非常大的文件时,如何强制刷新以便组件在每次迭代时顺利更新,而不是几乎每分钟更新一次?
谢谢。
最佳答案
使用 Task
,并调用 updateProgress()
以限制对 Platform.runLater()
调用的方式更新进度,以免淹没 FX 应用程序线程需要执行的更新太多。
所以你可以这样做:
public class ParseTask extends Task<Void> {
private final Path file ;
private long totalLines ;
public ParseTask(...) {
file = ... ;
totalLines = ... ;
}
public Void call() throws IOException {
// better to use AtomicLong here in case you parallelize the parsing
// at any point...
AtomicLong linesRead = new AtomicLong() ;
try (Stream<String> stream = Files.lines(file)) {
stream.forEach(line -> {
updateProgress(linesRead.incrementAndGet(), totalLines);
// do stuff with line...
});
}
return null ;
}
public long getTotalLines() {
return totalLines ;
}
}
然后
public class DxProgressIndicator {
private final VBox vb ;
private ProgressBar pb;
private ProgressIndicator pi;
private TextField tfNumbers;
private Label lblLineNumVal;
private Label lblAllLines;
private double progressIncrement;
private double progressPrevVal;
private int lineNum;
private Label lblFile;
private final DoubleProperty progress = new SimpleDoubleProperty();
public DoubleProperty progressProperty() {
return progress ;
}
public final double getProgress() {
return progressProperty().get();
}
public final void setProgress(double progress) {
progressProperty().set(progress);
}
public DxProgressIndicator() {
lblFile = new Label();
final HBox hbFile = new HBox();
hbFile.setAlignment(Pos.CENTER_LEFT);
Label lblCurrentFile = new Label("Current file: ");
lblCurrentFile.setMinWidth(90);
lblCurrentFile.setFont(Font.font(null, FontWeight.BOLD, 13));
hbFile.getChildren().addAll(lblCurrentFile, lblFile);
pb = new ProgressBar(0);
pb.setMinWidth(450);
pi = new ProgressIndicator(0);
pb.progressProperty().bind(progress);
pi.progressProperty().bind(progress);
final HBox hbPis = new HBox(10);
hbPis.setAlignment(Pos.CENTER_LEFT);
hbPis.getChildren().addAll(pb, pi);
lblLineNumVal = new Label();
lblLineNumVal.setMaxWidth(200);
Label slash = new Label(" / ");
lblAllLines = new Label();
final HBox hbLines = new HBox();
hbLines.setAlignment(Pos.CENTER_LEFT);
Label lblLineNum = new Label(" Currently parsing Line # : ");
lblLineNum.setFont(Font.font(null, FontWeight.BOLD, 12));
hbLines.getChildren().addAll(lblLineNum, lblLineNumVal, slash, lblAllLines);
tfNumbers = new TextField();
tfNumbers.setEditable(false);
tfNumbers.setMaxWidth(100);
final HBox hbTrxValues = new HBox();
hbTrxValues.setAlignment(Pos.CENTER_LEFT);
Label lblNumbers = new Label("(transaction, step, record) = ");
lblNumbers.setFont(Font.font(null, FontWeight.BOLD, 12));
hbTrxValues.getChildren().addAll(lblNumbers, tfNumbers);
vb = new VBox(10);
vb.getChildren().addAll(hbFile, hbPis, hbLines, hbTrxValues);
vb.setPadding(new Insets(10));
}
public Parent getRoot() {
return vb ;
}
public void setTotalLines(long totalLines) {
lblAllLines.setText(Long.toString(totalLines));
}
}
然后就
public class MyApp extends Application {
@Override
public void start(Stage primaryStage) {
ParseTask task = new ParseTask(...);
DxProgressIndicator indicator = new DxProgressIndicator();
indicator.setTotalLines(task.getTotalLines());
indicator.progressProperty().bind(task.progressProperty());
Scene scene = new Scene(indicator.getRoot());
primaryStage.setScene(scene);
primaryStage.setTitle("Dexter Parser");
primaryStage.show();
new Thread(task).start();
}
}
显然,您可能需要修改此细节 - 例如您可以执行 updateMessage(linesRead + "/"+ totalLines);
并将标签的文本绑定(bind)到任务的 messageProperty()
- 但它应该为您提供基本概念。
关于Javafx 进度更新不够快,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42098813/
killer = killer.Replace("(", "\("); 试图在大括号前放置'\',它显示“错误1无法识别的转义序列” 我还尝试在其前面加上“@”,但这会导致两个反斜杠。 最佳答案 使用
我创建了一个包含书籍的小型数据库,并试图在 C# 中使用正则表达式获取书名、作者和书籍年份,但出现错误。 数据库看起来像这样: Eragon // Christopher Paolini // 200
当我尝试编译时 scalala我得到一个 OutOfMemoryError: > compile [info] [info] == compile == [info] Source analysi
这个问题在这里已经有了答案: Regex escape with \ or \\? (5 个答案) 关闭 4 年前。 我遇到了 ) 不够的错误,但据我所知,我已经得到了所有的 )。 行获取错误: s
我正在尝试使用 pthread_create 函数创建一个线程。电话是这样的:res pthread_create(&threadID, &atributte, function, argument)
我需要能够遍历我的整个对象图并记录所有成员字段的所有内容。 例如:对象 A 具有对象 B 的集合,对象 B 具有对象 C 的集合,并且 A、B、C 上有附加字段,等等。 Apache Commons
我的 create.jspx 中有一行,如下所示: 此字段显示一个框,其中的行由 2 个实体之间的关系 @ManyToMany 填充。 现在的问题是,第一个实体只有一个填充此框的 String 属性
ctags -R dirName, vim -t Tags 非常强大,因为在这两个命令之后,你现在可以在该项目的代码之间导航,例如你可以使用 :tag functionName 跳转到某个函数的代码,
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: iphone 5 simulator - Cannot click on bottom of screen?
我有一个庞大的关键字表,一个关键字出现在一个外键中,例如 key=2 word=download key=3 word=download key=4 word=game 目前我有另一个字段称为字母索引
这个问题在这里已经有了答案: How to change stack size for a .NET program? (4 个答案) 关闭 7 年前。 我在 bmp 中有以下用于“blob 填充”
我是一名优秀的程序员,十分优秀!