Reputation: 1269
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
Reputation: 18096
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:
Overview of the javafx.concurrent Package
The Java platform provides a complete set of concurrency libraries available through the
java.util.concurrent
package. Thejavafx.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 theWorker
interface and two basic classes,Task
andService
, both of which implement theWorker
interface. TheWorker
interface provides APIs that are useful for a background worker to communicate with the UI. TheTask
class is a fully observable implementation of thejava.util.concurrent.FutureTask
class. TheTask
class enables developers to implement asynchronous tasks in JavaFX applications. TheService
class executes tasks.The
WorkerStateEvent
class specifies an event that occurs whenever the state of aWorker
implementation changes. Both theTask
andService
classes implement theEventTarget
interface and thus support listening to the state events.
The
Task
class defines a one-time object that cannot be reused. If you need a reusableWorker
object, use theService
class.
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