2017-08-15 13 views
1

今日は数時間のビデオの後にこの問題を解決できません。JavaFXバックグラウンドスレッドの問題

実行者サービス、ワーカー、タスク、並行パッケージ... ??私はどこで何をすべきかについて混乱しています。

起動時に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); 
    } 
}; 

答えて

1

実際の詳細については、記事を参照してください。Concurrency in JavaFX | JavaFX 2 Tutorials and Documentation, JavaFX 2.1, Irina Fedortsova記事から

いくつかの重要な引用符:javafx.concurrentの

  1. 概要パッケージ

    Javaプラットフォームはjava.util.concurrentパッケージを介して利用可能並行処理ライブラリの完全なセットを提供します。 javafx.concurrentパッケージは、JavaFXアプリケーションスレッドやGUI開発者が直面するその他の制約を考慮して、既存のAPIを活用します。

    javafx.concurrentパッケージはWorkerインターフェースとWorkerインタフェースを実装どちらも2つの基本的なクラス、TaskService、から成ります。 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); 
      

この情報がお役に立てば幸いです。

関連する問題