繁体   English   中英

如何在 JavaFX 中排队任务?

[英]How to queue tasks in JavaFX?

我使用 JavaFX 制作了一个 GUI,并且有三个单选按钮,一旦用户单击提交并创建另一个线程,并且根据选中的单选按钮,该线程运行所需的输出并将结果输出到控制台。

但是当线程正在运行时(完成一个进程大约需要 30 秒),我可以检查任何单选按钮。 它创建另一个线程并与另一个正在进行的线程一起输出 long 。 所以我的输出框只是一个杂乱无章的东西! 我正在查看异步任务,但我不确定这是否与之相关。

这是我需要的:如果一个任务正在运行,我在它运行时单击提交按钮,等待上一个任务结束,然后执行该任务。

这是我的代码的伪代码

class TestMain {

    //main

    public void main(String ... args)  {

        launch(args);
    }

    /*declaring a new textfield with name m_status update here*/
    

    /*once submit button is clicked*/{

        //create a new thread 
        //to run   
    }
}

class ThreadBlahBlah implements Runnable {

    if(/*first checkbox was selected*/){
        //do these fancy stuff
        Platform.runLater(new Runnable() {
                @Override
                public void run() {
                    TestMain.m_status_update.setText("Test Completed!");
                }
        });
    
    }else if(/*second checkbox was selected*/){
        //do these other fancy stuff
        Platform.runLater(new Runnable() {
                @Override
                public void run() {
                    TestMain.m_status_update.setText("Test Completed!");

                }
            });
    }
}

请不要建议我在任务运行时禁用单选按钮,因为我想像链接列表一样将任务排队。

使用单线程执行器来运行你的任务:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.concurrent.Task;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class QueuedTaskExample extends Application {

    private AtomicInteger taskCount = new AtomicInteger(0);

    private ExecutorService exec = Executors.newSingleThreadExecutor(r -> {
        Thread t = new Thread(r);
        t.setDaemon(true); // allows app to exit if tasks are running
        return t ;
    });

    // Use the following if you want the tasks to run concurrently, instead of consecutively:

    // private ExecutorService exec = Executors.newCachedThreadPool(r -> {
    //     Thread t = new Thread(r);
    //     t.setDaemon(true);
    //     return t ;
    // });


    @Override
    public void start(Stage primaryStage) {

        // Just keep track of number of tasks pending/running for a status label:
        IntegerProperty pendingTasks = new SimpleIntegerProperty(0);

        Button startButton = new Button("Start");
        TextArea textArea = new TextArea();
        textArea.setEditable(true);
        startButton.setOnAction(event -> {
            Task<Void> task = createTask();
            // add text to text area if task's message changes:
            task.messageProperty().addListener((obs, oldMessage, newMessage) -> {
                textArea.appendText(newMessage);
                textArea.appendText("\n");
            });

            // for maintaining status label:
            pendingTasks.set(pendingTasks.get()+1);
            task.setOnSucceeded(taskEvent -> pendingTasks.set(pendingTasks.get()-1));

            // run task in single-thread executor (will queue if another task is running):
            exec.submit(task);
        });

        // layout etc
        HBox controls = new HBox(startButton);
        controls.setAlignment(Pos.CENTER);
        controls.setPadding(new Insets(10));

        Label statusLabel = new Label();
        statusLabel.textProperty().bind(Bindings.format("Pending/running tasks: %s", pendingTasks));

        BorderPane root = new BorderPane(textArea, statusLabel, null, controls, null);
        Scene scene = new Scene(root, 600, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    @Override
    public void stop() {
        exec.shutdownNow();
    }

    // Trivial task that counts slowly to 5, updating its message as it goes:
    private Task<Void> createTask() {
        final int taskNumber = taskCount.incrementAndGet();
        return new Task<Void>() {
            @Override 
            public Void call() throws Exception {
                for (int count=1; count<=5; count++) {
                    Thread.sleep(1000);
                    updateMessage("Task "+taskNumber+": Count "+count);
                }
                return null ;
            }
        };
    }

    public static void main(String[] args) {
        launch(args);
    }
}

当用户单击单选按钮时,首先禁用所有单选按钮,这样用户在您的任务运行时将无法单击其他单选按钮。

完成后台作业后,重新启用所有单选按钮,以便用户可以选择其他任务。

请参阅Node.setDisabled()RadioButton扩展Node )。

如果你确实需要排队任务,你的后台线程应该维护一个任务列表,当用户点击时,将任务添加到后台线程应该消耗的列表中(如果当前任务完成并且还有更多任务,则启动另一个任务)。

有关高级线程执行,请参阅ExecutorsExecutorService

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM