相关依赖
io.reactivex
rxjava
1.3.8
toBlocking 的意思是主线程是否等待执行结果
需要注意的是flatMap后面的左括弧对应的右括弧 应该在subscribeOn之后。否则无法实现多线程的效果。
private static ExecutorService service= new ThreadPoolExecutor(4, 4,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
public static void main(String[] args) throws InterruptedException, IOException {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < 100; i++) {
list.add(i);
}
Observable.from(list)
.flatMap(num -> Observable
.fromCallable(
() -> {
Thread.sleep(10);
System.out.println(Thread.currentThread().getName() + "*****呃呃呃" + num);
//System.out.println(Thread.currentThread().getName()+"*****哈哈哈"+num);
return null;
}
)
// .subscribeOn(Schedulers.from(service))
.subscribeOn(Schedulers.from(service)))
// .onErrorResumeNext(response -> Observable.empty()).toList().toBlocking().single();
.toBlocking().subscribe()
;
System.out.println("#######MianEnd#############");
service.shutdown();
}