JavaFX .Join()关键字导致应用程序冻结一段时间

时间:2016-12-25 17:57:20

标签: java multithreading javafx

在完成Thread运行时,我需要执行少量线索。所以我在我想要稍后执行的线之前使用thread.join()关键字。

当我使用join()关键字运行程序时,My App界面会一直停滞,直到线程执行完成。我如何克服这个问题..?

实际上我的程序正在做的是,一旦我按下按钮它将执行方法调用Pass_data_from_java_to_report();,当它运行时,会出现一个单独的视图或Stage来通知用户该程序仍在工作。上述方法完成执行等待Stage将通过调用stage.close()关闭。这些操作工作正常,但使用join关键字冻结。

这是我的第一个方法。

if(event.getSource() == btnAdd){

   Stage stage     = Waiting();   // Returns Waiting Message
   Task <Void> task = new Task<Void>() {

       @Override
       protected Void call() throws Exception {           
       Pass_data_from_java_to_report();         // My Method     
       return null;
           }

      };
            // Displaying Waiting Message
            task.setOnRunning(eve -> {
                stage.show();
            });

            // Closing Waiting Message
            task.setOnSucceeded(e -> {
                stage.close();
            });

            // Thread for executing task
            Thread t1 = new Thread(() -> {
                task.run();
            });

            t1.start();
            t1.join();   // if i remove this buddy, there is no freezing 

           // below from here i want to execute upon thread completion
            System.out.println("Finish "); 

        }

第二种方法

if(event.getSource() == btnAdd){

    Stage stage = Waiting();

    Thread t1 = new Thread(() -> {
      Platform.runLater(() -> {
        stage.show();
       });
    });

   Thread t2 = new Thread(() -> {
     Pass_data_from_java_to_report();  // My Method
       Platform.runLater(() -> {
    // Closing the Stage running on another thread upon Above Method Completion
          stage.close();           
         });
     });

        t1.start();
        t2.start();

        t1.join();   // These are the Problems
        t2.join();   // this one too

      System.out.println("Finish ");

        }

以下是方法

     public void Pass_query_to_report(){    
        try {
              // Since method is not time consuming,  
                          // i added 3 sec sleep   before the Execution
             Thread.sleep(3000);
               /*
                    Some cord

               */
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
  1. 我怎样才能克服这个问题..?
  2. 哪种方法最好用..?是接近一个还是接近两个
  3. 在另一个帖子中关闭舞台是否安全,就像我做的那样......?它会自动关闭属于它的线程吗??
  4. (注意 - 我也尝试使用CountDownLatch但没有运气)

1 个答案:

答案 0 :(得分:4)

Thread.join()阻塞当前线程,直到指定的线程完成。由于您在负责呈现UI和处理用户事件的FX应用程序线程上调用它,因此它会阻止该线程,从而使UI无响应。

将任务完成后需要执行的代码移动到onSucceeded处理程序:

if(event.getSource() == btnAdd){

   Stage stage     = Waiting();   // Returns Waiting Message
   Task <Void> task = new Task<Void>() {

       @Override
       protected Void call() throws Exception {           
           Pass_data_from_java_to_report();         // My Method     
           return null;

       }

  };

    // Displaying Waiting Message
    task.setOnRunning(eve -> {
        stage.show();
    });

    // Closing Waiting Message
    task.setOnSucceeded(e -> {
        stage.close();

        // below from here i want to execute upon thread completion
        System.out.println("Finish "); 

    });

    // Thread for executing task
    Thread t1 = new Thread(task);

    t1.start();


}