gpt4 book ai didi

java - JavaFX 后台线程的问题

转载 作者:行者123 更新时间:2023-11-30 06:33:01 25 4
gpt4 key购买 nike

今天看完几个小时的视频后,我无法获取此信息。

执行器服务、worker、任务、并发包...??我很困惑不知道在哪里做什么。

我想初始化几个对象,这些对象在启动时将消息发送到 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);
}
};

最佳答案

具体详情请引用文章:Concurrency in JavaFX | JavaFX 2 Tutorials and Documentation, JavaFX 2.1, Irina Fedortsova .

文章中的一些重要引用:

  1. Overview of the javafx.concurrent Package

    The Java platform provides a complete set of concurrency libraries available through the java.util.concurrent package. The javafx.concurrent package leverages the existing API by considering the JavaFX Application thread and other constraints faced by GUI developers.

    The javafx.concurrent package consists of the Worker interface and two basic classes, Task and Service, both of which implement the Worker interface. The Worker interface provides APIs that are useful for a background worker to communicate with the UI. The Task class is a fully observable implementation of the java.util.concurrent.FutureTask class. The Task class enables developers to implement asynchronous tasks in JavaFX applications. The Service class executes tasks.

    The WorkerStateEvent class specifies an event that occurs whenever the state of a Worker implementation changes. Both the Task and Service classes implement the EventTarget interface and thus support listening to the state events.

  2. The Task class defines a one-time object that cannot be reused. If you need a reusable Worker object, use the Service class.

  3. A task can be started in one of the following ways:

    • By starting a thread with the given task as a parameter:

      Thread th = new Thread(task);
      th.setDaemon(true);
      th.start();
    • By using the ExecutorService API:

      ExecutorService.submit(task);

希望这有帮助。

关于java - JavaFX 后台线程的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45701294/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com