use of io.reactivex.observables.GroupedObservable in project RxJavaInAction by fengzhizi715.
the class RoundRobinForParallel2 method main.
public static void main(String[] args) {
final AtomicInteger batch = new AtomicInteger(0);
int threadNum = 5;
final ExecutorService executor = Executors.newFixedThreadPool(threadNum);
final Scheduler scheduler = Schedulers.from(executor);
Observable.range(1, 100).groupBy(new Function<Integer, Integer>() {
@Override
public Integer apply(@NonNull Integer integer) throws Exception {
return batch.getAndIncrement() % threadNum;
}
}).flatMap(new Function<GroupedObservable<Integer, Integer>, ObservableSource<?>>() {
@Override
public ObservableSource<?> apply(@NonNull GroupedObservable<Integer, Integer> integerIntegerGroupedObservable) throws Exception {
return integerIntegerGroupedObservable.observeOn(scheduler).map(new Function<Integer, String>() {
@Override
public String apply(@NonNull Integer integer) throws Exception {
return integer.toString();
}
});
}
}).subscribe(new Consumer<Object>() {
@Override
public void accept(@NonNull Object o) throws Exception {
System.out.println(o);
}
});
}
use of io.reactivex.observables.GroupedObservable in project RxJavaInAction by fengzhizi715.
the class RoundRobinForParallel1 method main.
public static void main(String[] args) {
final AtomicInteger batch = new AtomicInteger(0);
Observable.range(1, 100).groupBy(new Function<Integer, Integer>() {
@Override
public Integer apply(@NonNull Integer integer) throws Exception {
return batch.getAndIncrement() % 5;
}
}).flatMap(new Function<GroupedObservable<Integer, Integer>, ObservableSource<?>>() {
@Override
public ObservableSource<?> apply(@NonNull GroupedObservable<Integer, Integer> integerIntegerGroupedObservable) throws Exception {
return integerIntegerGroupedObservable.observeOn(Schedulers.io()).map(new Function<Integer, String>() {
@Override
public String apply(@NonNull Integer integer) throws Exception {
return integer.toString();
}
});
}
}).subscribe(new Consumer<Object>() {
@Override
public void accept(@NonNull Object o) throws Exception {
System.out.println(o);
}
});
}
Aggregations