JavaFX - 等待任务完成 [英] JavaFX - waiting for task to finish

查看:178
本文介绍了JavaFX - 等待任务完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JavaFX应用程序,可以实例化几个任务对象。

I have a JavaFX application which instantiates several Task objects.

目前,我的实现(见下文)调用行为 runFactory(),它在Task对象下执行计算。与此同时,调用 nextFunction()。有没有办法让 nextFunction()等待直到前一个任务完成?

Currently, my implementation (see below) calls the behavior runFactory() which performs computation under a Task object. Parallel to this, nextFunction() is invoked. Is there a way to have nextFunction() "wait" until the prior Task is complete?

我理解 thread.join( )等待正在运行的线程完成,但是使用GUI,由于事件调度线程,还有其他复杂层。
事实上,将 thread.join()添加到下面代码段的末尾只会停止UI交互。

I understand thread.join() waits until the running thread is complete, but with GUIs, there are additional layers of complexity due to the event dispatch thread. As a matter of fact, adding thread.join() to the end of the code-segment below only ceases UI interaction.

如果有任何建议如何让 nextFunction 等到它的上一个函数, runFactory 完成,我会非常感激。

If there are any suggestions how to make nextFunction wait until its prior function, runFactory is complete, I'd be very appreciative.

谢谢,

// 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();
}


推荐答案

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

When using Tasks you need to use setOnSucceeded and possibly setOnFailed to create a logic flow in your program, I propose that you also make runFactory() return the task rather than running it:

// 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();
    }
}

这篇关于JavaFX - 等待任务完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆