如何停止线程以使其上运行的任务永久停止?

时间:2017-12-14 11:43:37

标签: java multithreading javafx

我有一个javafx应用程序。我对它进行了编程,使应用程序以一个运行完成的进度条开始。完成后,窗口关闭,并打开登录窗口。登录后,登录窗口关闭,主窗口打开。

但这不是我的申请中发生的事情。

我创建了一个运行进度条的任务,从0到100,我完成了在线程上运行的任务。任务完成后,窗口关闭,登录窗口打开。

我遇到的问题是,当我使用登录窗口登录时,主窗口会打开,但登录窗口并没有关闭。

我在主窗口打开时编写了一个关闭登录窗口的方法,但是没有用。

请问我错过了什么?

这是任务类

public class TaskClass implements Runnable {

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            try {
                progress.setProgress(i / 100.0);
                Thread.sleep(100);

            } catch (InterruptedException ex) {
                Logger.getLogger(RunningController.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                // this is the method that closes the  window on which the progress bar is running
                closed(clos);  
                // this is the method that opens the login
                windowloadwindow("/inventory/management/login/InventoryManagementlogin.fxml", "DeMak Inventory");
            }
        });
    }
}

这会在线程上加载进度条

Thread thread;

@FXML
private JFXProgressBar progress; 

@Override
public void initialize(URL url, ResourceBundle rb) {
    progress.setProgress(0.0);
    Runnable tasks = new TaskClass();
    thread = new Thread(tasks);
    thread.start();
}

必须使用登录窗口登录后,才会发生这种情况

loadwindow("/inventory/management/ui/Main/main.fxml", "Deemax   Inventory");  // a method that opens the main window  

closed(closew);     // a method that closes the login window

1 个答案:

答案 0 :(得分:0)

为了同步 View view = LayoutInflater.from(context).inflate(R.layout.yourlayoutId,null); listView.setEmptyView(view); 代码和后台线程代码,您可以使用Platform.runLater()类,如下所示:

CountDownLatch

但是,相反,您可以更轻松地解决您的问题:

Task<Void> task = new Task<Void>() {
            @Override
            protected Void call() throws Exception {
                CountDownLatch sync = new CountDownLatch(1);  
                Platform.runLater(()->{
                    // GUI code
                    // ...
                    // end of GUI code
                    sync.countDown();
                });
                sync.await(); // This method will wait for sync.countDown() to be executed 
                // ...
                // code here will be executed after GUI code has finished
                // ...
                return null;
            }
        };
        Thread thread = new Thread(task);
        thread.start(); 

这是我的封闭方法

Task<Void> task = new Task<Void>() {
        @Override
        protected Void call() throws Exception {
            for (int i = 0; i < 100; i++) {
                final int finalCounter = i;
                Platform.runLater(()->{
                    try {
                        progress.setProgress(finalCounter / 100.0);
                    } catch (InterruptedException e) {
                        Logger.getLogger(RunningController.class.getName()).log(Level.SEVERE, null, ex);
                    }
                });
                Thread.sleep(100);
            }
            return null;
        }
        @Override
        protected void succeeded() {
            // code here will be executed after the thread had been completed
            Platform.runLater(()->{
                new Alert(Alert.AlertType.INFORMATION, "Thread Succeeded!").show();
            });
            closed(clos);      // this is the method that closes the  window on which the progress bar is running
            loadwindow("/inventory/management/login/InventoryManagementlogin.fxml", "DeMak Inventory");     // this is the method that opens the login window
            super.succeeded();
        }
        @Override
        protected void failed() {
            Platform.runLater(()->{
                new Alert(Alert.AlertType.ERROR, "Thread Failed!").show();
            });
            super.failed();
        }
    };
    Thread thread = new Thread(task);
    thread.start();

这是我的节点对象....它是一个包含X图像的vbox

         private void closed(Node nor) {
        Stage stage = (Stage)nor.getScene().getWindow();
        stage.close();
        }

所以我将节点传递给这样的方法。

  @FXML
private VBox clos;