【问题标题】:View wont update using ScheduledExecutorService使用 ScheduledExecutorService 查看不会更新
【发布时间】:2019-04-27 09:37:05
【问题描述】:

我在让 ScheduledExecutorService 工作时遇到了一些问题。

这是我正在调用的方法,如果我在没有调度的情况下调用它工作

但是,如果我通过 ScheduledExecutorService 调用它,我的屏幕不会更新,它也只会运行一次,所以我猜是抛出了异常,但我在服务器或客户端上都没有看到异常。

private void getNextRoundFromServer() throws IOException{
    try{
        if(!this.connected) return;
        this.dos.writeByte(2);
        this.dos.flush(); // Send data
        this.grid = (Grid) this.ois.readObject(); //i have checked, i do recieve this object
        setMainPane(render.render(this.grid)); //this renders the view
    } catch(ClassNotFoundException ex){
        ex.printStackTrace();
    }
}

这就是我的实现方式。

continuousPlayButton.setOnMouseClicked(e -> {
    try{
        Runnable scheduledRound = () -> {
            try {
                getNextRoundFromServer();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        };
        ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
        scheduler.scheduleAtFixedRate(scheduledRound, 1, 5, TimeUnit.SECONDS);
    }catch(Exception ex){
        ex.printStackTrace();
    }
});

为什么我的视图没有更新?

更新

解决方案:

正如用户tjanu 提到的:我忘记在计划任务上调用Platform.runLater。当我在没有安排的情况下调用它时我已经这样做了...... ????

【问题讨论】:

    标签: java javafx java-8


    【解决方案1】:

    您需要在 JavaFX 应用程序线程上进行 UI 更新。

    这就是Platform::runLater 的用途。

    -- 编辑 根据要求,此代码所需的更改:

    private void getNextRoundFromServer() throws IOException{
        try{
            if(!this.connected) return;
            this.dos.writeByte(2);
            this.dos.flush(); // Send data
            this.grid = (Grid) this.ois.readObject(); //i have checked, i do recieve this object
            // This is the needed change
            Platform.runLater(() -> setMainPane(render.render(this.grid))); //this renders the view
        } catch(ClassNotFoundException ex){
            ex.printStackTrace();
        }
    }
    

    【讨论】:

    • 该死,你是对的。不敢相信我错过了,一直在挠头 4 小时。我已经在我的其他 30 个电话中这样做了...... Argghhh。随时更新您的答案以包含代码。
    【解决方案2】:

    您在 getNextRoundFromServer() 方法中引用了实例变量,并且您现在从一个新的(匿名)Runnable 实例中调用该方法,因此您对“this”的引用可能无法解析为您认为它们在哪里解决。

    也许传入对原始实例的引用?

    private void getNextRoundFromServer(YourObjectClass instance) throws IOException{
        try{
            if(!instance.connected) return;
            instance.dos.writeByte(2);
            instance.dos.flush(); // Send data
            instance.grid = (Grid) instance.ois.readObject(); //i have checked, i do recieve this object
            setMainPane(render.render(instance.grid)); //this renders the view
        } catch(ClassNotFoundException ex){
            ex.printStackTrace();
        }
    }
    
    
    continuousPlayButton.setOnMouseClicked(e -> {
        try {
            final instanceRef = getReferenceToYourOriginalInstance();  
            Runnable scheduledRound = () -> {
                try {
                    getNextRoundFromServer( instanceRef );
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            };
            ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
            scheduler.scheduleAtFixedRate(scheduledRound, 1, 5, TimeUnit.SECONDS);
        }catch(Exception ex){
            ex.printStackTrace();
        }
    });
    

    【讨论】:

      【解决方案3】:

      不是您问题的完整答案,而是一个建议。也许您可以尝试覆盖 run() 方法。

      Runnable scheduledRound = new Runnable() {
          @Override
          public void run() {
              try {
                  getNextRoundFromServer();
              } catch (IOException ex) {
                  ex.printStackTrace();
              }
          }
      };
      

      【讨论】:

      • 注解,即@Override 不允许在那里 mydude。
      • 您似乎将匿名类与 lambda 组合在一起。
      • 我知道,要么之一。那么为什么你的答案是两者兼而有之呢?
      • 我用它来刷新 javafx 应用程序流式传输网络摄像头。 Unshure 如果这是一个不好的做法。虽然是简化
      • 现在至少可以编译,但问题仍然存在:为什么您认为这个建议是有益的?您基本上已经告诉@Joel 将他的 lambda 表达式转换为匿名类,但是这两个版本的代码在功能上是等效的。换句话说,这不应该对程序的逻辑产生任何影响。
      猜你喜欢
      • 2011-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多