silversunhunter
silversunhunter

Reputation: 1269

Trouble with JavaFX background threads

I am having trouble getting this after hours of videos today.

Executer service, worker, task, concurrent package...?? I am confused as to what to do where.

I want to initialize several objects which post messages to the UI as they fire up.

I have an interface:

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

One of my objects and some methods

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

My Main file that initializes several object on start.

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

Upvotes: 1

Views: 109

Answers (1)

Please refer to the article to learn the actual details: Concurrency in JavaFX | JavaFX 2 Tutorials and Documentation, JavaFX 2.1, Irina Fedortsova.

Some important quotes from the article:

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

Hope this helps.

Upvotes: 1

Related Questions