I’m using the Task class from JavaFx’s API to perform network operations. The class provides a progress system which seems perfectly adapted for this situation, allowing, for instance, to report the amount of files uploaded to the caller thread. To this end, I call updateMessage(x + “files uploaded”) every time a file has been successfully uploaded.
The problem is that the only message that I ever receive is the last one, when every file has been uploaded. I guess this phenomenon is explained in some way by the Javadoc:
Calls to updateMessage are coalesced and run later on the FX application thread, so calls to updateMessage, even from the FX Application thread, may not necessarily result in immediate updates to this property, and intermediate message values may be coalesced to save on event notifications.
How can I make it report correctly every, or at least some of the messages? I tried to add some Thread.sleep(20) around it but it’s not working. Any idea?
Most probably you are running Task on event thread, so it blocks updates. Task implements Runnable which is a bit misleading, but by looking in javadoc you can find out that correct way of running Task is assinging it to new Thread:
new Thread(task).start();See next example: