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

标签 java multithreading javafx lambda

我了解到在 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());

我发现了

讨论与此相关的一些问题并尝试:

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() 就可以了

关于Java FX Platform.runLater(() -> 相当于长时间运行的任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44817111/

相关文章:

java - (Java/Regex)如何找到 "http://"除了 "src="http ://"

Java ssh (jsch) 将流设置为 UI 而不是控制台

java - 指令表现不符合预期

java - 让runnable进入 hibernate 状态

java - 为什么 MouseEvent 在 JavaFX 中为 getX() 和 getSceneX() 返回相同的坐标

java - 在java中重命名文件/文件夹时如何区分文件和文件夹

c++ - 在并发数据结构中,什么级别的锁定粒度是好的?

linux - 使用 kernel_thread 创建内核线程

javafx - 标签不更新任务进度

java - 如何发出和处理自定义事件?