我想知道当我并行访问数据库表时是否应该使用 Schedulers.io() 还是 Schedulers.newThread() 。
例如,如果我使用 Schedulers.io() 并行地从数千个表中选择记录,则任务完成后会在线程池中创建很多新创建的线程。
Observabe.just("table1", "table2", "table3"...)
.flatMap(t -> {
// creating the observable that emits the record
return Observable create(s -> {
Record rec = selectFrom(t);
s.onNext(rec);
s.onCompleted();
})
.subscribeOn(Schedulers.io()); // select in parallel
})...
但是,如果此类任务是每晚批处理,则这些线程不必位于线程池中,因为它们中的大多数在大多数情况下都不需要。
在这种情况下,我认为我应该使用 Schedulers.newThread()。这样对吗?
如果你想限制 io 并行工作,那么使用自定义Scheduler
:
Scheduler scheduler = Schedulers.from(Executors.newFixedThreadPool(6));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)