我有以下反应代码:
//JavaFX ProgressBar
ProgressBar progressBar = new ProgressBar(0);
Flowable
.fromIterable(itemList)
.parallel()
.runOn(Schedulers.io())
.map(item -> {
Thread.sleep(1000); //imitate expensive computation
return item.getCode();
})
.sequential()
.observeOn(JavaFxScheduler.platform())
.subscribe(result -> {
System.out.println(result);
});
我想在flowable的每次迭代中更新ProgressBar。
我想到了这样的事情:
int i= 0; //increment on every iteration
int total = itemList.size(); //total number if items in List
progressBar.setProgress((float)(i / total));
但是,我无法弄清楚如何在每次迭代结束时增加变量i
。
答案 0 :(得分:1)
只需在订阅中增加i
AtomicInteger i = new AtomicInteger(0);
Flowable
.fromIterable(itemList)
.parallel()
.runOn(Schedulers.io())
.map(item -> {
Thread.sleep(1000); //imitate expensive computation
return item.getCode();
})
.sequential()
.observeOn(JavaFxScheduler.platform())
.subscribe(result -> {
i.increment();
progressBar.setProgress((float)(i.get()/itemList.size))
System.out.println(result);
});