Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

FutureTask<Callable> t = new FutureTask<>(...);
Platform.runLater(t);

while (!t.isDone()) { } // wait for the FutureTask to be called

long end = System.nanoTime();

However, it seems the FX Application Thread might be handling the FutureTask before it handles the rest of the keypress events.

TLDR: I want to precisely measure when the JavaFX Application Thread has finished handling the four keypress events I generate.

How can I go about this? Thank you!

like image 815
rosstex Avatar asked Oct 30 '22 05:10

rosstex


1 Answers

Use ExecutorService and wait until your threads are done. Store a timestamp of when you started the service and then compare the difference between the two times to get your answer.

A simple example on how to use the ExecutorService:

ExecutorService taskExecutor = Executors.newFixedThreadPool(4);
while(...) {
  taskExecutor.execute(new MyTask());
}
taskExecutor.shutdown();
try {
  taskExecutor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
  ...
}
like image 72
MSB Avatar answered Nov 15 '22 03:11

MSB