add isTimeout property for RunAsyncDaemon
This commit is contained in:
@ -1,8 +1,8 @@
|
|||||||
package com.imyeyu.fx.task;
|
package com.imyeyu.fx.task;
|
||||||
|
|
||||||
import javafx.concurrent.Task;
|
|
||||||
import com.imyeyu.java.bean.timi.TimiCode;
|
import com.imyeyu.java.bean.timi.TimiCode;
|
||||||
import com.imyeyu.java.bean.timi.TimiException;
|
import com.imyeyu.java.bean.timi.TimiException;
|
||||||
|
import javafx.concurrent.Task;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 异步守护执行,如果 {@link RunAsync} 超过指定时间没有回调,将触发 onTimeout
|
* 异步守护执行,如果 {@link RunAsync} 超过指定时间没有回调,将触发 onTimeout
|
||||||
@ -17,6 +17,9 @@ public abstract class RunAsyncDaemon<T> extends RunAsync<T> {
|
|||||||
/** true 为回调成功 */
|
/** true 为回调成功 */
|
||||||
protected boolean isCallBack;
|
protected boolean isCallBack;
|
||||||
|
|
||||||
|
/** true 为回调超时 */
|
||||||
|
protected boolean isTimeout;
|
||||||
|
|
||||||
/** 默认构造器(执行超时 2 秒) */
|
/** 默认构造器(执行超时 2 秒) */
|
||||||
public RunAsyncDaemon() {
|
public RunAsyncDaemon() {
|
||||||
this(2000);
|
this(2000);
|
||||||
@ -35,21 +38,23 @@ public abstract class RunAsyncDaemon<T> extends RunAsync<T> {
|
|||||||
protected Task<T> createTask() {
|
protected Task<T> createTask() {
|
||||||
super.createTask();
|
super.createTask();
|
||||||
|
|
||||||
|
isTimeout = false;
|
||||||
isCallBack = false;
|
isCallBack = false;
|
||||||
task.setOnSucceeded(e -> {
|
task.setOnSucceeded(e -> {
|
||||||
|
isCallBack = true;
|
||||||
onFinish();
|
onFinish();
|
||||||
onFinish(lastT);
|
onFinish(lastT);
|
||||||
onFinally();
|
onFinally();
|
||||||
isCallBack = true;
|
|
||||||
});
|
});
|
||||||
task.setOnFailed(e -> {
|
task.setOnFailed(e -> {
|
||||||
onFinally();
|
|
||||||
isCallBack = true;
|
isCallBack = true;
|
||||||
|
onFinally();
|
||||||
});
|
});
|
||||||
task.setOnRunning(e -> new Thread(() -> {
|
task.setOnRunning(e -> new Thread(() -> {
|
||||||
try {
|
try {
|
||||||
Thread.sleep(timeout);
|
Thread.sleep(timeout);
|
||||||
if (!isCallBack) {
|
if (!isCallBack) {
|
||||||
|
isTimeout = true;
|
||||||
onTimeout();
|
onTimeout();
|
||||||
}
|
}
|
||||||
} catch (InterruptedException ex) {
|
} catch (InterruptedException ex) {
|
||||||
|
|||||||
Reference in New Issue
Block a user