我有几个任务,它们的progressProperty始终为-1。根据我的经验,-1通常意味着一些东西,但是我在文档中找不到任何有关它的信息。有人知道这意味着什么吗?还是我设定进度的错误?
为了使这个问题更多地涉及我的个人问题,您必须知道我将多个任务的progressProperty结合在一起,并使用doubleBinding将其绑定到进度栏。根据要求,这里有一些代码,为了使代码最小化,我删除了一些东西,例如导入声明,main方法并标记了重要部分。
public class X extends Application implements EventHandler<ActionEvent> {
private Task<Void> taskRun;
private Y y;
private ExecutorService exec; // <-- Executor Service
private Stage dialog;
private Stage window;
//Interface inter;
Scene scene;
private ProgressBar progress; // <-- Progress Bar
@Override
public void start(Stage primaryStage) {
y = new Y();
window = primaryStage;
exec = Executors.newCachedThreadPool(); // <-- Executor Service
progress = new ProgressBar(0.0); // <-- Progress Bar
VBox vb = new VBox();
vb.getChildren().add(progress);
scene = new Scene(vb);
window.setScene(scene);
window.show();
run();
}
private DoubleBinding progressBinding = null; // <-- Double binding
private void run() {
taskRun = new Task<Void>() {
@Override
public void run() {
try {
ConverterManager();
} catch (InterruptedException ex) {
Logger.getLogger(X.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
protected Void call() throws Exception {
throw new UnsupportedOperationException("Not supported yet.");
}
};
// I need to use the progressProperty of this task first
progressBinding = taskRun.progressProperty().multiply(1);
// Binding doubleBinding to progressBar
progress.progressProperty().bind(progressBinding);
exec.execute(taskRun);
}
private void ConverterManager() throws InterruptedException {
progressBinding = null; // <-- Reset doubleBinding
int anz = 5;
for (int i = 0; i < anz; i++) {
Task<Void> mt = new Task<Void>() {
@Override
public void run() {
try {
// I use another class and have to update
// progress there, I do so with a BiConsumer
y.setProgressUpdate(this::updateProgress);
// Here I call a Method from the other class
// the String parameter is, because I use
// a reflection there
y.startZ("Z");
} catch (Exception ex) {
Logger.getLogger(X.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
protected Void call() throws Exception {
throw new UnsupportedOperationException("Not supported yet.");
}
};
// Here I combine the progress of the different ProgressProperties
// Also, I notized that mt.ProgressProperty.get() is -1 for all tasks
DoubleBinding scaledProgress = mt.progressProperty().divide(anz);
if (progressBinding == null) {
progressBinding = scaledProgress;
} else {
// Adding the scaled down progress of the Task to the
// doubleBinding that is connected to the progressBar
progressBinding = progressBinding.add(scaledProgress);
}
exec.execute(mt);
};
}
@Override
public void handle(ActionEvent event) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
这是Y班
public class Y {
private BiConsumer<Integer, Integer> progressUpdate;
public void setProgressUpdate(BiConsumer<Integer, Integer> progressUpdate) {
this.progressUpdate = progressUpdate;
}
void startZ(String className) throws Exception {
Class cls = Class.forName("stackoverflow." + className);
Object obj = cls.newInstance();
Class[] noParams = new Class[0];
Class[] progressParams = new Class[1];
progressParams[0] = BiConsumer.class;
Method bindProgress = cls.getDeclaredMethod("setProgressUpdate", progressParams);
bindProgress.invoke(obj, progressUpdate);
Method method = cls.getDeclaredMethod("start", noParams);
method.invoke(obj);
}
}
这是Z类:
public class Z
{
private BiConsumer<Integer, Integer> progressUpdate;
public void setProgressUpdate(BiConsumer<Integer, Integer> progressUpdate) {
this.progressUpdate = progressUpdate ;
}
public void start() {
int gesamt = 1000;
for (int i = 1; i < gesamt; i++) {
if (progressUpdate != null)
progressUpdate.accept(i, gesamt);
}
}
}
最佳答案
看看java doc
值-1表示无法确定当前进度
(即不确定)。此属性可能会或可能不会更改
从默认值-1开始,具体取决于特定的Worker
实施。