【问题标题】:JavaFX - waiting for task to finishJavaFX - 等待任务完成
【发布时间】:2013-03-05 06:44:00
【问题描述】:

我有一个 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();
}

【问题讨论】:

    标签: task javafx


    【解决方案1】:

    使用任务时,您需要使用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();
        }
    }
    

    【讨论】:

    • Dreen,我喜欢你的逻辑......谢谢。这很有意义。
    • 这个解决方案看起来很有效。也就是说,与将Platform.runLater(nextFunction()) 之类的操作作为工厂线程的最后一步相比,这种方法的优势是什么?
    • 感谢一百万,setOnFailed() 在两天的挣扎后救了我
    • 如果我需要同步等待任务(使用 ExecutorServcie),我是否也可以使用 Future 的 get() 方法,或者这在 JavaFX 中是不是一种不好的做法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    相关资源
    最近更新 更多