栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在JavaFX中排队任务?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在JavaFX中排队任务?

使用单线程执行程序运行任务:

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/428569.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号