【问题标题】:Trouble with JavaFX background threadsJavaFX 后台线程的问题
【发布时间】:2017-08-15 20:48:00
【问题描述】:

今天播放了数小时的视频后,我无法获取此内容。

Executer service、worker、task、并发包...??我对在哪里做什么感到困惑。

我想初始化几个在启动时向 UI 发送消息的对象。

我有一个界面:

public interface SystemMessage {
    void postMessage(String outText);
}

我的一个对象和一些方法

public class Identity extends Service {
    private String machineId = null;

    private static SystemMessage systemMessage;

    public Identity(SystemMessage smInterface){
        systemMessage = smInterface;

        //how do i run the identity class in the background and report to the UI?
        // --------------------------------------------------
        //
        systemMessage.postMessage("Checking Machine Identity");
        if (getStoredIdentity()){
            systemMessage.postMessage("Machine ID exists.");
        }
        else{
            systemMessage.postMessage("No Machine ID. Create New.");
            machineId = createUuid();
            storeIdentity();
        }
    }
}

// --------------------------------------------------
//
//Do I create individual tasks for each method in the class? do i use service, task, executer, or????
// --------------------------------------------------
//

private void storeIdentity(){
    Properties p = new Properties();
    p.setProperty("machineId", this.machineId);
    try {
        FileWriter file = new FileWriter("identity.properties");
        p.store(file, "Identity");
        systemMessage.postMessage("New Identity Created and Stored.");
    } catch (IOException e) {
        systemMessage.postMessage("Error Creating New Identity!");
        e.printStackTrace();
    }
}

在启动时初始化多个对象的我的主文件。

@Override
public void start(Stage primaryStage) throws Exception{
    FXMLLoader loader = new FXMLLoader(getClass().getResource("fxml/entry.fxml"));
    Parent root = loader.load();
    mainController = (SystemMessage) loader.getController();
    primaryStage.setTitle("ASI Sync!");
    primaryStage.setScene(new Scene(root, 300, 275));
    primaryStage.show();
    initializeSync.start();
}

Thread initializeSync = new Thread (){
    public void run(){
        System.out.println("Thread Running");
        Identity identity = new Identity(mainController);
        Api api = new Api(mainController);
        SqLite db = new SqLite(mainController);
    }
};

【问题讨论】:

    标签: java multithreading javafx


    【解决方案1】:

    具体内容请参考文章:Concurrency in JavaFX | JavaFX 2 Tutorials and Documentation, JavaFX 2.1, Irina Fedortsova

    文章中的一些重要引述:

    1. javafx.concurrent 包概述

      Java 平台通过java.util.concurrent 包提供了一套完整的并发库。 javafx.concurrent 包通过考虑 JavaFX 应用程序线程和 GUI 开发人员面临的其他限制来利用现有 API。

      javafx.concurrent 包由Worker 接口和两个基本类TaskService 组成,它们都实现了Worker 接口。 Worker 接口提供了对后台工作人员与 UI 通信有用的 API。 Task 类是java.util.concurrent.FutureTask 类的完全可观察的实现。 Task 类使开发人员能够在 JavaFX 应用程序中实现异步任务。 Service 类执行任务。

      WorkerStateEvent 类指定每当Worker 实现的状态更改时发生的事件。 TaskService 类都实现了EventTarget 接口,因此支持监听状态事件。

    2. Task 类定义了一个不能重复使用的一次性对象。如果您需要可重用的 Worker 对象,请使用 Service 类。

    3. 可以通过以下方式之一启动任务:

      • 通过以给定任务作为参数启动线程:

        Thread th = new Thread(task);
        th.setDaemon(true);
        th.start();
        
      • 通过使用 ExecutorService API:

        ExecutorService.submit(task);
        

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-08
      • 2011-06-18
      • 1970-01-01
      • 1970-01-01
      • 2015-05-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多