简体   繁体   English

为什么在JavaFX 2.2服务中两次调用了我的计划方法

[英]Why is my scheduled method called twice in a JavaFX 2.2 Service

See the following example: 请参见以下示例:

public class Test extends Application {
  public static void main(final String[] args) {
    launch(args);
  }

  @Override
  public void start(final Stage primaryStage) {
    primaryStage.setTitle("Hi!");
    primaryStage.setScene(new Scene(new BorderPane()));
    primaryStage.show();

    Service<Void> myService = new Service<Void>() {
      @Override
      protected Task<Void> createTask() {
        return new Task<Void>() {
          @Override
          protected void cancelled() {
            super.cancelled();
            System.out.println(Thread.currentThread().getName() + " - cancelled() called");
          }

          @Override
          protected void failed() {
            super.failed();
            System.out.println(Thread.currentThread().getName() + " - failed() called");
          }

          @Override
          protected void running() {
            super.running();
            System.out.println(Thread.currentThread().getName() + " - running() called");
          }

          @Override
          protected void succeeded() {
            super.succeeded();
            System.out.println(Thread.currentThread().getName() + " - succeeded() called");
          }

          @Override
          protected void scheduled() {
            super.scheduled();
            System.out.println(Thread.currentThread().getName() + " - scheduled() called");
          }

          @Override
          protected Void call() throws Exception {
            System.out.println(Thread.currentThread().getName() + " - call() called");
            return null;
          }
        };
      }
    };
    myService.stateProperty().addListener(new ChangeListener<State>() {
      @Override
      public void changed(ObservableValue<? extends State> arg0, State arg1, State newValue) {
        System.out.println("State: " + newValue);                   
      }
    });
    myService.start();
  }
}

This gives the following output: 这给出以下输出:

State: SCHEDULED 状态:已排定
JavaFX Application Thread - scheduled() called JavaFX应用程序线程-Scheduled()称为
Thread-4 - call() called 线程4-call()被调用
JavaFX Application Thread - scheduled() called JavaFX应用程序线程-Scheduled()称为
State: RUNNING 状态:RUNNING
JavaFX Application Thread - running() called JavaFX应用程序线程-running()称为
State: SUCCEEDED 状态:成功
JavaFX Application Thread - succeeded() called JavaFX应用程序线程-成功调用了()

Note that this line occurs twice: 请注意,此行出现两次:

JavaFX Application Thread - scheduled() called JavaFX应用程序线程-Scheduled()称为

This means the "scheduled()" method is called twice. 这意味着“ scheduled()”方法被调用两次。 I would have expected the method to be called only once. 我希望该方法仅被调用一次。 Am I doing something wrong, is this is bug, do I misinterpret the API? 我做错什么了吗,这是bug,我会误解API吗?

Thanks! 谢谢!

The lifecycle of the Worker object is defined as follows. Worker对象的生命周期定义如下。 When created, the Worker object is in the READY state. 创建后,Worker对象处于READY状态。 Upon being scheduled for work, the Worker object transitions to the SCHEDULED state. 在安排工作后,Worker对象将转换为SCHEDULED状态。 After that, when the Worker object is performing the work, its state becomes RUNNING. 之后,当Worker对象正在执行工作时,其状态将变为RUNNING。 Note that even when the Worker object is immediately started without being scheduled, it first transitions to the SCHEDULED state and then to the RUNNING state. 请注意,即使在没有计划的情况下立即启动Worker对象,它也会先转换为SCHEDULED状态,然后再转换为RUNNING状态。

from this Concurency JavaFX 来自此Concurency JavaFX

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM