Java FX Platform.runLater(() -> 等效于长时间运行的任务



我了解到在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用于任务接口
  • javafx和线程管理差异的摇摆计时器替代
  • 带有lambda表达式的线程

讨论围绕此问题的一些问题,并尝试了:

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现在包装:

  1. 线程
  2. 任务
  3. 可呼叫
  4. 潜在的例外/可投掷
  5. 任务结果的运行时类

这5个项目中的一些可能是多余的,并且可以从标准Java概念中获得。我认为至少在从一个衬里运行事情后可以快速访问这些。

希望这能达到工作状态,并感谢您的帮助!

只有 new Thread(() -> dosomething()).start()应该做技巧

这是一个传统的XY问题。

Task不仅仅是背景线程,因此您可以使用常规线程。这是财产的美丽!

使用Task的真正好处是,所有状态更改和进度更新都可以安全地观察并绑定到实时场景,同时在其他线程上进行所有背景工作。这是班级进行重型工作并致电Platform.runLater的工作。

您需要一个子类而不是可运行的原因,因此您可以调用其受保护的updateXxx()方法,而不必担心线程问题。

这样说,如果这是单行代码,您将没有任何好处。为此使用简单线程。

希望这会有所帮助。

这样做将使您失去将内容更新回到Task类本身支持的UI线程的能力。另一方面,我确实同意,如果您想在" do and and-forget"样式中做某事,这将很有用。

问题就像您所说的 - 您没有添加new Thead()Thread.start() IN。执行此操作:

public static void runInBackground(Runnable runnable) {
    Task<Void> task = new Task<>() {
        @Override
        public Void call() throws Exception {
            runnable.run();
            return null;
        }
    };
    new Thead(task).start();
}
runInBackground(() -> System.out.println(Thread.currentThread().hashCode()));

请注意,您的Task不再是非空间,因为它现在无法返回任何内容。您的lambda需要能够引用 Task对象以异步返回结果 - 使用lambda是不可能的。

现在的答案是基于Damianos提示的问题。

我发现的例外的解决方法是

com.sun.javafx.application.PlatformImpl.startup(() -> {
});

,但似乎有点骇客...

最新更新