Wait for JavaFX Application Thread to complete event processing?

Many questions are asked here about how to suspend a JavaFX application thread for a background thread, but I want the opposite!

I am trying to check how long it takes to fully process a number of key inputs. I use the Automaton test library for JavaFX, and editor.type (key) generates a keystroke event that is handled 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 that the FX application thread can handle FutureTask before it handles the rest of the keystroke events.

TL; DR: I want to accurately measure when the JavaFX Application Thread finished processing the four keypress events that I generate.

How can i do this? Thanks!

+5
source share
1 answer

Use ExecutorService and wait for your threads to finish. Save the timestamp when you start the service, and then compare the difference between the two points to get an answer.

A simple example of using ExecutorService :

 ExecutorService taskExecutor = Executors.newFixedThreadPool(4); while(...) { taskExecutor.execute(new MyTask()); } taskExecutor.shutdown(); try { taskExecutor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); } catch (InterruptedException e) { ... } 
+2
source

Source: https://habr.com/ru/post/1247803/


All Articles