dimanche 24 avril 2016

Wait for JavaFX Application Thread to finish handling events?

Lots of questions on here are asking about how to pause the JavaFX Application thread for a background thread, but I want the opposite!

I'm trying to test how long it takes for a series of key inputs to be fully handled. I am using the Automaton testing library for JavaFX, and editor.type(key) generates a keypress event that gets processed by the application. Here is one of many attempts:

long start = System.nanoTime();
editor.type(AGUtils.LEFT_ARROW);
editor.type(AGUtils.LEFT_ARROW);
editor.type(AGUtils.RIGHT_ARROW);
editor.type(AGUtils.RIGHT_ARROW);
Runnable x = () -> {
    System.out.println("Free");
};
FutureTask<Runnable> t = new FutureTask<>(x, null);
Platform.runLater(t);
while (!t.isDone()) {
    System.out.println("waiting");
}
long end = System.nanoTime();
long diff = end - start;
studentTotal += diff;
System.out.println("Took " + Long.toString(diff) + "ns");

However, it seems the FX Application Thread might be handling the FutureTask before it handles the rest of the keypress events. I've tried many other variations on this code snippet, which I will not reproduce here, but all were in vain.

How can I go about this?

Aucun commentaire:

Enregistrer un commentaire