简体   繁体   中英

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!

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) {
  ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM