javafx - Javafx 中 'ScheduledService' 的简单示例

标签 javafx scheduled-tasks

我是一名学生,从一个月开始学习 JavaFX。
我正在开发一个应用程序,我希望服务在执行任务后重复启动。为此,我知道' ScheduledService ' 用来。
因此,任何人都可以用简单的示例解释 scheduleservice 的使用,以及它与 JavaFX 中的“服务”有何不同。谢谢 ;)

编辑 :我如何定义这个名为 DataThread 的 ScheduledService 应该每 5 秒重新启动一次?

public class DataThread extends ScheduledService<Void>
{
    @Override
    public Task<Void> createTask() {
        return new Task<Void>() {
            @Override
            public Void call() throws Exception {
             for(i=0;i<10;i++)
             {
                 System.out.println(""+i);
             }
              return null;
            }
        };
    }
}  

最佳答案

考虑到您对 Service 有充分的了解类(class)。 ScheduledService 只是一个具有调度功能的服务。

来自文档

The ScheduledService is a Service which will automatically restart itself after a successful execution, and under some conditions will restart even in case of failure



所以我们可以说,
Service -> Execute One Task
ScheduledService -> Execute Same Task at regular intervals

定时服务的一个非常简单的例子是 TimerService,它计算服务任务被调用的次数。计划每 1 秒调用一次
import java.util.concurrent.atomic.AtomicInteger;

import javafx.application.Application;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.concurrent.ScheduledService;
import javafx.concurrent.Task;
import javafx.concurrent.WorkerStateEvent;
import javafx.event.EventHandler;
import javafx.stage.Stage;
import javafx.util.Duration;

public class TimerServiceApp extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        TimerService service = new TimerService();
        AtomicInteger count = new AtomicInteger(0);
        service.setCount(count.get());
        service.setPeriod(Duration.seconds(1));
        service.setOnSucceeded(new EventHandler<WorkerStateEvent>() {

            @Override
            public void handle(WorkerStateEvent t) {
                System.out.println("Called : " + t.getSource().getValue()
                        + " time(s)");
                count.set((int) t.getSource().getValue());
            }
        });
        service.start();
    }

    public static void main(String[] args) {
        launch();
    }

    private static class TimerService extends ScheduledService<Integer> {
        private IntegerProperty count = new SimpleIntegerProperty();

        public final void setCount(Integer value) {
            count.set(value);
        }

        public final Integer getCount() {
            return count.get();
        }

        public final IntegerProperty countProperty() {
            return count;
        }

        protected Task<Integer> createTask() {
            return new Task<Integer>() {
                protected Integer call() {
                    //Adds 1 to the count
                    count.set(getCount() + 1);
                    return getCount();
                }
            };
        }
    }
}

关于javafx - Javafx 中 'ScheduledService' 的简单示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27853735/

相关文章:

c# - .net 应用程序在通过计划任务触发时失败

java - 如何在 Linux 中终止和停止在应用程序外运行的 TimerTask

java - 打包非模块化JavaFX应用程序

java - anchor 不遵循多边形 - JavaFX

javafx - JSoup 随机抛出 java.io.IOException : stream is closed when running from browser

java - 如何将显示 SVG 的 WebView 缩放到固定像素高度?

JavaFX 8 - 自定义控件及其子控件

java - 每次Windows启动时安排任务的vbs代码是什么?

python - Crontab 不适用于 python

python - 通过Windows Scheduler运行python脚本不起作用