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

亨利

运行Thread完成后,我几乎不需要执行任何操作因此thread.join(),我在想要稍后执行的连接线之前使用了关键字。

当我使用join()关键字运行程序时,“我的应用程序”界面会卡住,直到线程执行完成。我该如何克服这个问题呢?

实际上,我的程序正在执行的操作是,一旦按下按钮,它将执行方法调用,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. 以我完成该操作的方式关闭在另一个线程中启动的舞台是否安全?它会自动关闭属于它的线程吗?

(注意-我也尝试使用CountDownLatch但没有运气)

詹姆斯_D

Thread.join()阻塞当前线程,直到指定线程完成。由于您是在负责渲染UI和处理用户事件的FX Application Thread上调用它的,因此它将阻止该线程,从而使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();


}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

一段时间后,libgdx游戏开始冻结

来自分类Dev

Lubutnu 16.04冻结了一段时间

来自分类Dev

一段时间后,Apache Ignite缓存冻结

来自分类Dev

SSH 会话在一段时间后冻结

来自分类Dev

Parallel.ForEach 一段时间后冻结

来自分类Dev

tkinter 窗口在一段时间后冻结

来自分类Dev

一段时间后套接字超时

来自分类Dev

一段时间后套接字超时

来自分类Dev

使用一段时间后,我的应用在滚动WebView时冻结,并说“无法锁定表面”

来自分类Dev

JavaFX冻结问题

来自分类Dev

Javafx Alert使程序冻结

来自分类Dev

JavaFX冻结窗口

来自分类Dev

为什么运行我的python脚本时powershell冻结了一段时间

来自分类Dev

当我在大量的TableViewCells上搜索时,EarlGrey冻结了一段时间

来自分类Dev

使用10.000项更新h:selectManyListbox时,Ajax渲染“冻结”一段时间

来自分类Dev

为什么运行我的python脚本时powershell冻结了一段时间

来自分类Dev

用10.000项更新h:selectManyListbox时,Ajax渲染“冻结”一段时间

来自分类Dev

为什么一段时间后我的笔记本电脑突然冻结?

来自分类Dev

ubuntu 16.04-一段时间后变慢并冻结

来自分类Dev

当我在大量的TableViewCells上搜索时,EarlGrey冻结了一段时间

来自分类Dev

一段时间后Arduino滚动文本程序冻结

来自分类Dev

Android LocationListener一段时间

来自分类Dev

一段时间后翻转

来自分类Dev

暂停CCNode一段时间

来自分类Dev

禁用按钮一段时间

来自分类Dev

一段时间之间的Grep

来自分类Dev

隐藏div一段时间

来自分类Dev

有一段时间了

来自分类Dev

运行 if 语句一段时间