Class RunAsyncScheduled<T>

java.lang.Object
javafx.concurrent.Service<T>
javafx.concurrent.ScheduledService<T>
com.imyeyu.fx.task.RunAsyncScheduled<T>
All Implemented Interfaces:
javafx.concurrent.Worker<T>, javafx.event.EventTarget

public abstract class RunAsyncScheduled<T> extends javafx.concurrent.ScheduledService<T>
异步任务,支持多次调度
    // ---------- 快速构造 ----------

    // 每秒执行一次,参数 2 不是 FX 线程
    // call(Duration, Callback)
    RunAsyncScheduled.call(Duration.seconds(1), () -> {});

    // 每秒执行一次,参数 2 是 FX 线程
    // finish(Duration, Callback)
    RunAsyncScheduled.finish(Duration.seconds(1), () -> {});

    // 动态周期执行,参数 2 不是 FX 线程
    // call(CallbackArg, Callback)
    RunAsyncScheduled.call(scheduled -> {}, () -> {});

    // 动态周期执行,参数 2 是 FX 线程
    // finish(CallbackArg, Callback)
    RunAsyncScheduled.finish(scheduled -> {}, () -> {});

    // 每秒执行一次并返回,参数 3 (FX 线程)的入参是参数 2 (非 FX 线程)的返回
    // call(Duration, CallbackReturn, CallbackArg)
    RunAsyncScheduled.call(Duration.seconds(1), () -> "result", result -> {});

    // ---------- 标准构造 ----------
    new RunAsyncScheduled<T>() {

        @Override
        protected void onScheduled() {
            // 任务开始后执行,此时可调整任务的周期和延时
        }

        @Override
        protected T call() {
            // 这里不是 FX 线程,可以执行一些长时间的事情

            update(new T()); // 主动更新数据
            progress(.6);    // 更新进度
            return new T();  // 返回更新数据
        }

        @Override
        protected void onUpdate(T t) {
            // 数据更新事件,属于 FX 线程,视 UI 线程状态回调
        }

        @Override
        protected void onUpdateProgress(double progress) {
            // 更新进度事件,属于 FX 线程,视 UI 线程状态回调
        }

        @Override
        protected void onFinish() {
            // 线程执行完成,属于 FX 线程
        }

        @Override
        protected void onException(Throwable e) {
            // 线程异常,属于 FX 线程
        }
    }.start();  <-- 千万别忘了启动任务
Since:
2022-01-14 17:05
Author:
夜雨
  • Nested Class Summary

    Nested classes/interfaces inherited from interface javafx.concurrent.Worker

    javafx.concurrent.Worker.State
  • Property Summary

    Properties inherited from class javafx.concurrent.ScheduledService

    backoffStrategy, cumulativePeriod, currentFailureCount, delay, lastValue, maximumCumulativePeriod, maximumFailureCount, period, restartOnFailure

    Properties inherited from class javafx.concurrent.Service

    exception, executor, message, onCancelled, onFailed, onReady, onRunning, onScheduled, onSucceeded, progress, running, state, title, totalWork, value, workDone
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected boolean
     
    protected T
    最近一次更新数据
    protected PublicTask<T>
    执行任务

    Fields inherited from class javafx.concurrent.ScheduledService

    EXPONENTIAL_BACKOFF_STRATEGY, LINEAR_BACKOFF_STRATEGY, LOGARITHMIC_BACKOFF_STRATEGY
  • Constructor Summary

    Constructors
    Constructor
    Description
    默认构造器
    RunAsyncScheduled(javafx.util.Duration period)
    标准构造
  • Method Summary

    Modifier and Type
    Method
    Description
    protected T
    执行事件(非 FX 线程)
    call(com.imyeyu.java.bean.CallbackArg<RunAsyncScheduled<Void>> scheduleEvent, com.imyeyu.java.bean.Callback asyncCallback)
    快速构造动态周期调度任务
    call(javafx.util.Duration period, com.imyeyu.java.bean.Callback asyncCallback)
    快速构造多次调度任务
    static <T> RunAsyncScheduled<T>
    call(javafx.util.Duration period, com.imyeyu.java.bean.CallbackReturn<T> asyncCallback, com.imyeyu.java.bean.CallbackArg<T> finishCallback)
    快速构造多次调度任务
    protected javafx.concurrent.Task<T>
     
    finish(com.imyeyu.java.bean.CallbackArg<RunAsyncScheduled<Void>> scheduleEvent, com.imyeyu.java.bean.Callback finishCallback)
    快速构造多次调度任务
    finish(javafx.util.Duration period, com.imyeyu.java.bean.Callback asyncCallback)
    快速构造多次调度任务
    void
    中断任务
    protected void
    发生异常(FX 线程)
    protected void
    线程完成监听,无论正常完成还是出现异常,都会触发此方法
    protected void
    完成事件(FX 线程,只要线程结束就会调用)
    protected void
    完成事件(FX 线程,只要线程结束就会调用,参数为 update(T)call() 最后一次更新值)
    protected void
    call() 运行时触发
    protected void
    每次开始运行时触发,在延时计算前触发
    protected void
    数据更新事件,update(T)call() 返回参数触发,属于 FX 线程
    protected void
    onUpdateProgress(double progress)
    更新进度事件,progress(double) 触发,属于 FX 线程
    protected void
    progress(double progress)
    主动更新进度,非 FX 线程的 call 里触发,onUpdateProgress(double) 里视 UI 线程状态回调
    protected void
    update(T t)
    主动更新数据,通常是非 FX 线程的 call 里触发,onUpdate(T) 里视 UI 线程状态回调

    Methods inherited from class javafx.concurrent.ScheduledService

    backoffStrategyProperty, cancel, cumulativePeriodProperty, currentFailureCountProperty, delayProperty, executeTask, failed, getBackoffStrategy, getCumulativePeriod, getCurrentFailureCount, getDelay, getLastValue, getMaximumCumulativePeriod, getMaximumFailureCount, getPeriod, getRestartOnFailure, lastValueProperty, maximumCumulativePeriodProperty, maximumFailureCountProperty, periodProperty, reset, restartOnFailureProperty, setBackoffStrategy, setDelay, setMaximumCumulativePeriod, setMaximumFailureCount, setPeriod, setRestartOnFailure, succeeded

    Methods inherited from class javafx.concurrent.Service

    addEventFilter, addEventHandler, buildEventDispatchChain, cancelled, exceptionProperty, executorProperty, fireEvent, getException, getExecutor, getMessage, getOnCancelled, getOnFailed, getOnReady, getOnRunning, getOnScheduled, getOnSucceeded, getProgress, getState, getTitle, getTotalWork, getValue, getWorkDone, isRunning, messageProperty, onCancelledProperty, onFailedProperty, onReadyProperty, onRunningProperty, onScheduledProperty, onSucceededProperty, progressProperty, ready, removeEventFilter, removeEventHandler, restart, running, runningProperty, scheduled, setEventHandler, setExecutor, setOnCancelled, setOnFailed, setOnReady, setOnRunning, setOnScheduled, setOnSucceeded, start, stateProperty, titleProperty, totalWorkProperty, valueProperty, workDoneProperty

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • task

      protected PublicTask<T> task
      执行任务
    • lastT

      protected T lastT
      最近一次更新数据
    • isInterrupted

      protected boolean isInterrupted
  • Constructor Details

    • RunAsyncScheduled

      public RunAsyncScheduled()
      默认构造器
    • RunAsyncScheduled

      public RunAsyncScheduled(javafx.util.Duration period)
      标准构造
      Parameters:
      period - 周期
  • Method Details

    • createTask

      protected javafx.concurrent.Task<T> createTask()
      Specified by:
      createTask in class javafx.concurrent.Service<T>
    • call

      protected T call() throws Exception
      执行事件(非 FX 线程)
      Returns:
      处理结果
      Throws:
      Exception - 处理异常
    • onScheduled

      protected void onScheduled()
      每次开始运行时触发,在延时计算前触发
    • onRunning

      protected void onRunning()
      call() 运行时触发
    • update

      protected void update(T t)
      主动更新数据,通常是非 FX 线程的 call 里触发,onUpdate(T) 里视 UI 线程状态回调
      Parameters:
      t - 更新值
    • progress

      protected void progress(double progress)
      主动更新进度,非 FX 线程的 call 里触发,onUpdateProgress(double) 里视 UI 线程状态回调
      Parameters:
      progress - 进度值
    • onUpdate

      protected void onUpdate(T t)
      数据更新事件,update(T)call() 返回参数触发,属于 FX 线程
      Parameters:
      t - 更新数据
    • onUpdateProgress

      protected void onUpdateProgress(double progress)
      更新进度事件,progress(double) 触发,属于 FX 线程
      Parameters:
      progress - 进度值
    • onFinish

      protected void onFinish()
      完成事件(FX 线程,只要线程结束就会调用)
    • onFinish

      protected void onFinish(T t)
      完成事件(FX 线程,只要线程结束就会调用,参数为 update(T)call() 最后一次更新值)
      Parameters:
      t - 执行事件返回值
    • onFinally

      protected void onFinally()
      线程完成监听,无论正常完成还是出现异常,都会触发此方法
    • onException

      protected void onException(Throwable e)
      发生异常(FX 线程)
      Parameters:
      e - 异常
    • interrupt

      public void interrupt()
      中断任务
    • call

      public static RunAsyncScheduled<Void> call(javafx.util.Duration period, com.imyeyu.java.bean.Callback asyncCallback)
      快速构造多次调度任务
      Parameters:
      period - 周期
      asyncCallback - 异步线程执行
      Returns:
      任务
    • finish

      public static RunAsyncScheduled<Void> finish(javafx.util.Duration period, com.imyeyu.java.bean.Callback asyncCallback)
      快速构造多次调度任务
      Parameters:
      period - 周期
      asyncCallback - 异步线程回调
      Returns:
      任务
    • call

      public static <T> RunAsyncScheduled<T> call(javafx.util.Duration period, com.imyeyu.java.bean.CallbackReturn<T> asyncCallback, com.imyeyu.java.bean.CallbackArg<T> finishCallback)
      快速构造多次调度任务
      Type Parameters:
      T - 执行数据回调
      Parameters:
      period - 周期
      asyncCallback - 异步线程执行
      finishCallback - 异步线程执行完成回调
      Returns:
      任务
    • call

      public static RunAsyncScheduled<Void> call(com.imyeyu.java.bean.CallbackArg<RunAsyncScheduled<Void>> scheduleEvent, com.imyeyu.java.bean.Callback asyncCallback)
      快速构造动态周期调度任务
      Parameters:
      scheduleEvent - 执行前调度,可重置执行周期
      asyncCallback - 异步线程执行
      Returns:
      任务
    • finish

      public static RunAsyncScheduled<Void> finish(com.imyeyu.java.bean.CallbackArg<RunAsyncScheduled<Void>> scheduleEvent, com.imyeyu.java.bean.Callback finishCallback)
      快速构造多次调度任务
      Parameters:
      scheduleEvent - 执行前调度,可重置执行周期
      finishCallback - 异步线程执行完成回调
      Returns:
      任务