Multithreading 包含线程的进度条在javafx中第二次未运行

Multithreading 包含线程的进度条在javafx中第二次未运行,multithreading,javafx,progress-bar,task,runnable,Multithreading,Javafx,Progress Bar,Task,Runnable,我有一个进度条,它在我的应用程序中第一次正常运行。进度条根据列表的大小运行。当我更改ObservalArrayList的大小并再次运行它或从其他页面返回页面时,它无法正常工作。我只能在页面中获取文本和不确定的进度条。未显示进程计数和运行时间。我已经把这个逻辑放在了一个任务中。当我将日志行放在特定位置时,我发现有时它不会进入任务调用方法中的某些位置,如Platform.runlaternew Runnable。请在下面查找代码。我正在为UI部分使用scenebuilder public class

我有一个进度条,它在我的应用程序中第一次正常运行。进度条根据列表的大小运行。当我更改ObservalArrayList的大小并再次运行它或从其他页面返回页面时,它无法正常工作。我只能在页面中获取文本和不确定的进度条。未显示进程计数和运行时间。我已经把这个逻辑放在了一个任务中。当我将日志行放在特定位置时,我发现有时它不会进入任务调用方法中的某些位置,如Platform.runlaternew Runnable。请在下面查找代码。我正在为UI部分使用scenebuilder

public class ProgressBarController implements Initializable {

public static final Stage stage ;
@FXML
ProgressBar progressBar;
@FXML
public static Button cancelButton;
@FXML
Button save;
@FXML
Label totalprocess;
@FXML
Label runningprocess;



/**
 * Initializes the controller class.
 */
@Override
public void initialize(URL url, ResourceBundle rb) {

    final Task task;
    task = new Task<Void>() {
        @Override
        protected Void call() throws Exception {

            for (int i = 0; i < rightObservableArrayList.size(); i++) {

                final int k = i + 1;
                //setting the value for total number of processes
                totalprocess.setText("total number of process");
                //setting the value for the current process. It gets updated as the loop increments.
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        runningprocess.setText(Integer.toString(k));
                    }
                });

                /*
                  some processing for the current val 
                */

                //update the progressBar
                updateProgress(i + 1, <totalprocesssize>);
                updateMessage(String.valueOf(i));
                Platform.runLater(new Runnable() {
                    @Override
                    public void run() {
                        /*
                         some code to display the total time to run the process
                        */
                    }
                });
            }
            return null;
        }
    };
    progressBar.progressProperty().bind(task.progressProperty());
    Label labelCount = new Label();
    labelCount.textProperty().bind(task.messageProperty());
    new Thread(task).start();
    task.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
        @Override
        public void handle(WorkerStateEvent t) {

              ///some code
            }
        }
    });
}
@FXML
    public void cancelFired(ActionEvent event) throws IOException {
        cancelFlag = true;
        progress.setScene(cancelButton.getScene());
        progress.close();
    }
任何关于为什么会发生这种情况的想法都会非常有用。请分享您的想法。

来自:

与FutureTask一样,任务是一次性类,不能重用。 请参阅以获取可重用的工作人员


您是在两次运行同一个任务实例,还是在创建一个新实例?我已使用cancelFired方法更新了代码,当我从UI中单击cancelButton时,该方法将被触发。当我单击Cancel按钮时,cancelFlag(静态)被设置为true,任务内的for循环检查此标志的true状态并中断循环。