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

时间:2017-06-29 05:39:14

标签: 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表达式在同一个Thread和

中运行
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概念中获得。我认为至少可以方便地从一个衬里运行东西后快速访问这些东西。

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

4 个答案:

答案 0 :(得分:2)

只需要new Thread(() -> dosomething()).start()就可以了解

答案 1 :(得分:1)

这是一种传统的XY problem

Task不仅仅是一个后台线程,因此您可以使用常规线程。这是物业的美丽!

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

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

有了这个说法,如果这是一个单行代码,你将没有任何好处。为此使用简单的线程。

希望这有帮助。

答案 2 :(得分:1)

执行此操作会导致您无法将内容更新回public function changePasswordOtp($user_phone_number) { $user = User::where('phone_number', '=', $user_phone_number)->count(); if($user > 0) { $this->sendOtp($user_phone_number); //return response with sent otp } else { return Response::json( $this->respondFailure('User not found') ); } } 类本机支持的UI线程。另一方面,我同意如果你想在背景中做一些事情,那么这可能是有用的。“做到 - 忘记”&#34;风格。

问题就像你说的那样 - 你没有添加Tasknew Thead()。执行此操作:

Thread.start()

请注意,您的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())); 不能再无效,因为它现在无法返回任何内容。你的lambda需要能够引用Task对象来异步返回结果 - 使用lambda永远不可能。

答案 3 :(得分:-1)

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

我发现的异常的解决方法是

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

但似乎有点hacky ......

相关问题