task - JavaFX-等待任务完成

标签 task javafx

我有一个JavaFX应用程序,该应用程序实例化了几个Task对象。

当前,我的实现(请参见下文)调用行为 runFactory(),该行为在Task对象下执行计算。与此并行,调用 nextFunction()。有没有办法让 nextFunction()“等待”,直到完成上一个任务?

我了解 thread.join()等到正在运行的线程完成后,但是使用GUI时,由于事件分发线程而导致了额外的复杂性。
实际上,将 thread.join()添加到下面的代码段的末尾只会停止UI交互。

如果有任何建议如何使 nextFunction 等到其上一个函数 runFactory 完成,我将不胜感激。

谢谢,

// High-level class to run the Knuth-Morris-Pratt algorithm.
public class AlignmentFactory {
    public void perform() {
        KnuthMorrisPrattFactory factory = new KnuthMorrisPrattFactory();
        factory.runFactory();   // nextFunction invoked w/out runFactory finishing.
        // Code to run once runFactory() is complete.
        nextFunction()   // also invokes a Task.
        ...
    }
}

// Implementation of Knuth-Morris-Pratt given a list of words and a sub-string.
public class KnuthMorrisPratt {
   public void runFactory() throws InterruptedException {
       Thread thread = null;
       Task<Void> task = new Task<Void>() {

           @Override public Void call() throws InterruptedException {
           for (InputSequence seq: getSequences) {
                KnuthMorrisPratt kmp = new KnuthMorrisPratt(seq, substring);
                kmp.align();

            }
            return null; 
        }
    };
    thread = new Thread(task);
    thread.setDaemon(true);
    thread.start();
}

最佳答案

使用Tasks时,您需要使用setOnSucceeded以及可能的setOnFailed在程序中创建逻辑流,我建议您还使runFactory()返回该任务而不是运行它:

// Implementation of Knuth-Morris-Pratt given a list of words and a sub-string.
public class KnuthMorrisPratt {
   public Task<Void> runFactory() throws InterruptedException {
       return new Task<Void>() {

       @Override public Void call() throws InterruptedException {
       for (InputSequence seq: getSequences) {
        KnuthMorrisPratt kmp = new KnuthMorrisPratt(seq, substring);
        kmp.align();

        }
        return null; 
    }
    };
}

// High-level class to run the Knuth-Morris-Pratt algorithm.
public class AlignmentFactory {
    public void perform() {
    KnuthMorrisPrattFactory factory = new KnuthMorrisPrattFactory();
    Task<Void> runFactoryTask = factory.runFactory();
    runFactoryTask.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
        @Override
        public void handle(WorkerStateEvent t)
        {
            // Code to run once runFactory() is completed **successfully**
            nextFunction()   // also invokes a Task.
        }
    });

    runFactoryTask.setOnFailed(new EventHandler<WorkerStateEvent>() {
        @Override
        public void handle(WorkerStateEvent t)
        {
            // Code to run once runFactory() **fails**
        }
    });
    new Thread(runFactoryTask).start();
    }
}

关于task - JavaFX-等待任务完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15217442/

相关文章:

android - 已安装 GnuPG,但来自 gradle 任务的 "command not found"

JavaFX开启新场景

java - 为什么 ReadOnlyDoubleProperty 不是 Double?

intellij-idea - 显示 FileChooser 对话框时为 "java[934:22850] +[CATransaction synchronize] called within transaction"

java - AutoCompletionBinding 无法访问类 com.sun.javafx.event.EventHandlerManager

Java生产者/消费者,检测处理结束

c# - 'ArrayList' 不包含 'GetAwaiter' 的定义

c# - 确保调用函数两次时返回 C# 异步任务的最佳方法是什么?

c# - 如何在不更改签名的情况下将同步方法转换为异步方法

JavaFX 脚本似乎无法在其他计算机上运行