简体   繁体   中英

ExecutorService - fill up progress bar in JavaFX with the number of finished tasks

I have a code which sends files concurrently via ExecutorService. However, I want to have a progress bar in JavaFX, which would fill up with the number of files sent.

How would I achieve so?

I'm creating tasks via List<Callable<String>> clientTasks = new ArrayList<>();, and lauching them via es.invokeAll(clientTasks);

Thanks.

You can do something along the following lines:

IntegerProperty completed = new SimpleIntegerProperty(0);
List<Callable<String>> wrappedTasks = new ArrayList<>();
for (Callable<String> task : clientTasks) {
    wrappedTasks.add(() -> {
        String result = task.call();
        Platform.runLater(() -> completed.set(completed.get()+1));
        return result ;
    });
}
completed.addListener((obs, oldValue, newValue) -> 
    progressBar.setProgress(1.0 * newValue.intValue() / clientTasks.size()));
es.invokeAll(wrappedTasks);

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