Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java FX Platform.runLater(()->;相当于长时间运行的任务_Java_Multithreading_Javafx_Lambda - Fatal编程技术网

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

Java FX Platform.runLater(()->;相当于长时间运行的任务,java,multithreading,javafx,lambda,Java,Multithreading,Javafx,Lambda,我在JavaFX中了解到 SwingUtilities.invokeLater(新的Runnable(){ 公开募捐{ dosomething(); } }); 可能只是 Platform.runLater(()->{dosomething()}; 对于一项长期运行的任务,我了解到您需要使用以下任务来包装: Task Task=新任务(){ @凌驾 公开作废通知(){ dosomething(); } }; 新线程(任务).start(); 现在,如果能有一个类似的lambda快捷方式,那

我在JavaFX中了解到

SwingUtilities.invokeLater(新的Runnable(){
公开募捐{
dosomething();
}
});
可能只是

Platform.runLater(()->{dosomething()};
对于一项长期运行的任务,我了解到您需要使用以下任务来包装:

Task Task=新任务(){
@凌驾
公开作废通知(){
dosomething();
}
};
新线程(任务).start();
现在,如果能有一个类似的lambda快捷方式,那就太好了

TaskLaunch.start(() -> dosomething());
我发现

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

package com.bitplan.task.util;
导入java.util.concurrent.Callable;
导入javafx.concurrent.Task;
/**
*这是一个实用程序任务,用于启动具有lambda表达式的任务
* 
*@author wf
*
*/
公共类任务启动{
/**
* 
*@param可调用
*@返回新任务
*/
公共静态任务任务(可调用可调用){
任务=新任务(){
@凌驾
public T call()引发异常{
返回callable.call();
}
};
返回任务;
}
}
使用JUnit测试:

整数计数器=0;
布尔运行=假;
公共整数增量(){
运行=真;
(跑步时){
计数器++;
试一试{
睡眠(1);
}捕捉(中断异常e){
}
}
返回计数器;
}
/**
*@抛出异常
*/
@试验
public void testTaskLaunch()引发异常{
// https://stackoverflow.com/questions/30089593/java-fx-lambda-for-task-interface
Task Task=TaskLaunch.Task(()->increment());
试一试{
睡眠(20);
}捕捉(中断异常e){
//
}
运行=错误;
assertTrue(task.get()>10);
}
这还没有达到我想看到的效果,问题似乎是 lambda表达式在同一线程中运行,而

new Thread(task).start();
部分需要整合

需要什么来获得(至少接近)上面提到的短一衬板?

是一个

可行吗

基于@Damianos提案

我试过:

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;
  }

}
至少TaskLaunch现在包装了:

  • 线
  • 任务
  • 可调用
  • 潜在的例外/可丢弃的
  • 任务结果的运行时类
  • 这5个项目中的一些可能是多余的,并且可以从标准java概念中获得。我认为至少在从一行程序运行之后快速访问这些项目是很方便的


    希望这能进入工作状态,感谢您的帮助!

    刚刚
    新线程(()->dosomething())。开始()
    应该会做这个把戏。刚刚
    新线程(()->dosomething())。开始()
    应该会做这个把戏。这是一种传统的做法

    一个
    任务
    不仅仅是一个背景线程,因此你可以使用常规线程。这是属性的美妙之处

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

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

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


    希望这能有所帮助。

    这是一种传统做法

    一个
    任务
    不仅仅是一个背景线程,因此你可以使用常规线程。这是属性的美妙之处

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

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

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


    希望这有帮助。

    这样做会使您失去将内容更新回
    任务
    类本机支持的UI线程的能力。另一方面,我同意,如果您想在后台以“做而忘”的方式做一些事情,这会很有用

    问题与您所说的一样-您没有在中添加
    new 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()));
    
    publicstaticvoid运行背景(Runnable-Runnable){
    任务=新任务(){
    @凌驾
    public Void call()引发异常{
    runnable.run();
    返回null;
    }
    };
    新Thead(task.start();
    }
    runInBackground(()->System.out.println(Thread.currentThread().hashCode());
    

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

    这样做会导致您失去将内容更新回UI thr的能力ead本机由
    任务
    类支持。另一方面,我同意如果您想在后台以“做而忘”的方式做一些事情,这会很有用

    问题与您所说的一样-您没有在中添加
    new 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()));
    
    publicstaticvoid运行背景(Runnable-Runnable){
    任务=新任务(){
    @凌驾
    public Void call()抛出
    
    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)
    
    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()));
    
    com.sun.javafx.application.PlatformImpl.startup(() -> {
    });