5

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!

rosstex
  • 773
  • 1
  • 9
  • 23
  • refer to this question: http://stackoverflow.com/questions/7939257/wait-until-all-threads-finish-their-work-in-java/36797569#36797569 – Ravindra babu May 02 '16 at 16:21

1 Answers1

2

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) {
  ...
}
MSB
  • 854
  • 7
  • 24
  • Thank you so much for your response! Mind if I ask about the approach you've mentioned? I am able to generate inputs that get handled by FX EventHandlers, but I don't exactly know how those events are represented in JavaFX's internals before the Application thread calls the relevant handle() methods in my program. I had thought using Platform.runLater() would insert an event into the queue in successive order (after the keypresses), but I was wrong. How would I use an ExecutorService in this situation? Thanks! – rosstex May 02 '16 at 07:54
  • I added a basic example on how to use the service. I would need more time to write a more elaborate one. – MSB May 02 '16 at 08:03
  • Instead of writing an entirely new code example this answer might help you out further if my answer isn't satisfactory: http://stackoverflow.com/questions/33394428/javafx-version-of-executorservice. – MSB May 02 '16 at 08:16
  • Thanks, I'll pour over both of them carefully! – rosstex May 02 '16 at 08:18