Javafx Task间歇性地运行

时间:2015-01-15 21:13:55

标签: java concurrency javafx-8

我正在运行FX8(JDK8_u25)并拥有以下代码

private void initializeBrokerCommunication()
{
    System.out.println("0");
    try {
        Task<List<Integer>> initTask = new Task<List<Integer>>() {
            @Override
            protected List<Integer> call() throws Exception
            {
                System.out.println("1");
                Thread.sleep(500);
                DateTime upperBound = new DateTime(masterXAxis.getUpperBound());
                DateTime lowerBound = new DateTime(masterXAxis.getLowerBound());
                int seconds = Seconds.secondsBetween(lowerBound, upperBound).getSeconds();
                lowerBound = lowerBound.minusSeconds(seconds);
                System.out.println("2");
                return broker.getDeviceData(null, upperBound.toDate(), lowerBound.toDate(), true);

            }
        };
        // setOnSucceeded ensures that the method processHistoricData runs on the Fx thread.
        initTask.setOnSucceeded(event -> {
            System.out.println("3");
            processHistoricData(initTask.valueProperty().getValue(), true);
        });
        // submit the task and the scheduler decides when to run it.
        scheduler.submit(initTask);
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println("4");
}

schedulerScheduledExecutorService对象,初始化如下 ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(5);

initializeBrokerCommunication()方法包含在扩展Application类的类中。我的班级的多次运行每次都会产生不同的输出。

0-4-1-2-3是我的期望,但有时我会看到0-4正在打印,即使被打印也看不到1-2-3

关于为什么任务间歇性运行的任何指示。

0 个答案:

没有答案