如下代码所示,我有 4 个函数,我手动设置进度监视器的每个进度间隔 = 25%。
但是,如果我有 20 个或更多功能,我的方法手动设置进度间隔将更加不切实际。
在给定 X 个函数的情况下,设置进度间隔的更好方法是什么?
class Task extends SwingWorker<Void, Void> {
@Override
public Void doInBackground() {
int progress = 0;
setProgress(0);
while (progress < 100 && !isCancelled())
{
functionA();
try {
Thread.sleep(1000);
}catch(InterruptedException ex) {
ex.printStackTrace();
}
progress += 25;
setProgress(Math.min(progress, 100));
taskOutput.setText("Step 2");
functionB();
progress += 25;
setProgress(Math.min(progress, 100));
try {
Thread.sleep(1000);
}catch(InterruptedException ex) {
ex.printStackTrace();
}
taskOutput.setText("Step 3");
functionC();
progress += 25;
setProgress(Math.min(progress, 100));
try {
Thread.sleep(1000);
}catch(InterruptedException ex) {
ex.printStackTrace();
}
taskOutput.setText("Step 4");
functionD();
try {
Thread.sleep(1000);
}catch(InterruptedException ex) {
ex.printStackTrace();
}
progress += 25;
setProgress(Math.min(progress, 100));
}
return null;
}