【发布时间】:2017-06-29 05:39:13
【问题描述】:
我了解到在 JavaFX 中相当于
SwingUtilities.invokeLater(new Runnable() {
public void run() {
dosomething();
}
});
可能只是
Platform.runLater(() ->{ dosomething()};
对于一项长期运行的任务,我了解到您需要使用以下任务来包装事物:
Task<Void> task = new Task<Void>() {
@Override
public Void call() {
dosomething();
}
};
new Thread(task).start();
现在如果能有类似的 lambda 快捷方式,那就太好了
TaskLaunch.start(() -> dosomething());
我找到了
- JAVA FX - Lambda for Task interface
- Swing timer alternative for JavaFX and the thread management difference
- Thread with Lambda expression
围绕这个讨论一些问题并尝试:
package com.bitplan.task.util;
import java.util.concurrent.Callable;
import javafx.concurrent.Task;
/**
* this is a utility task to launch tasks with lambda expressions
*
* @author wf
*
*/
public class TaskLaunch {
/**
*
* @param callable
* @return the new task
*/
public static <T> Task<T> task(Callable<T> callable) {
Task<T> task = new Task<T>() {
@Override
public T call() throws Exception {
return callable.call();
}
};
return task;
}
}
使用 JUnit 测试:
Integer counter=0;
boolean running=false;
public Integer increment() {
running=true;
while (running) {
counter++;
try {
Thread.sleep(1);
} catch (InterruptedException e) {
}
}
return counter;
}
/**
* @throws Exception
*/
@Test
public void testTaskLaunch() throws Exception {
// https://stackoverflow.com/questions/30089593/java-fx-lambda-for-task-interface
Task<Integer> task=TaskLaunch.task(() -> increment());
try {
Thread.sleep(20);
} catch (InterruptedException e) {
//
}
running=false;
assertTrue(task.get()>10);
}
这还不是我想看到的。问题似乎是 lambda 表达式在同一个线程中运行,而
new Thread(task).start();
部分需要整合。
需要什么才能获得(至少接近)上面提到的短线?
是一个
TaskLaunch.start(() -> dosomething());
可行吗?
基于@Damianos 提案https://stackoverflow.com/a/44817217/1497139
我试过了:
package com.bitplan.task;
import java.util.concurrent.Callable;
import javafx.concurrent.Task;
/**
* this is a utility task to launch tasks with lambda expressions
*
* @author wf
*
*/
public class TaskLaunch<T> {
Thread thread;
Task<T> task;
Callable<T> callable;
Throwable throwable;
Class<T> clazz;
public Thread getThread() {
return thread;
}
public void setThread(Thread thread) {
this.thread = thread;
}
public Task<T> getTask() {
return task;
}
public void setTask(Task<T> task) {
this.task = task;
}
public Callable<T> getCallable() {
return callable;
}
public void setCallable(Callable<T> callable) {
this.callable = callable;
}
public Throwable getThrowable() {
return throwable;
}
public void setThrowable(Throwable throwable) {
this.throwable = throwable;
}
public Class<T> getClazz() {
return clazz;
}
public void setClazz(Class<T> clazz) {
this.clazz = clazz;
}
/**
* construct me from a callable
*
* @param callable
*/
public TaskLaunch(Callable<T> callable, Class<T> clazz) {
this.callable = callable;
this.task = task(callable);
this.clazz = clazz;
}
/**
*
* @param callable
* @return the new task
*/
public static <T> Task<T> task(Callable<T> callable) {
Task<T> task = new Task<T>() {
@Override
public T call() throws Exception {
return callable.call();
}
};
return task;
}
/**
* start
*/
public void start() {
thread = new Thread(task);
thread.start();
}
/**
* start the given callable
* @param callable
* @param clazz - the return Type class
* @return - the launch result
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public static TaskLaunch start(Callable<?> callable, Class<?> clazz) {
TaskLaunch<?> launch = new TaskLaunch(callable, clazz);
launch.start();
return launch;
}
}
并将测试更改为:
/**
* @throws Exception
*/
@SuppressWarnings("unchecked")
@Test
public void testTaskLaunch() throws Exception {
// https://stackoverflow.com/questions/30089593/java-fx-lambda-for-task-interface
TaskLaunch<Integer> launch = TaskLaunch.start(()->increment(),Integer.class);
try {
Thread.sleep(20);
} catch (InterruptedException e) {
//
}
running=false;
assertTrue(launch.getTask().get()>10);
}
这与我的目标很接近,但我得到了:
java.lang.IllegalStateException: Toolkit not initialized
at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:273)
at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:268)
at javafx.application.Platform.runLater(Platform.java:83)
at javafx.concurrent.Task.runLater(Task.java:1225)
at javafx.concurrent.Task$TaskCallable.call(Task.java:1417)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.lang.Thread.run(Thread.java:745)
至少 TaskLaunch 现在包装:
- 线程
- 任务
- 可调用
- 潜在的异常/可抛出
- 任务结果的运行时类
这 5 项中的一些可能是多余的,并且可以从标准 Java 概念中获得。我认为至少在从一个班轮运行后快速访问这些内容很方便。
希望这可以正常工作并感谢您的帮助!
【问题讨论】:
-
Platform.runLater 用于从非 GUI 线程更新 GUI,一旦 GUI 线程空闲,它就会负责更新 GUI,因为任务用于非数据密集型任务与 GUI 相关,您希望它们在后台运行,这样它就不会冻结应用程序
标签: java multithreading javafx lambda