Search in sources :

Example 1 with GroupedObservable

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);
        }
    });
}
Also used : Scheduler(io.reactivex.Scheduler) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Function(io.reactivex.functions.Function) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NonNull(io.reactivex.annotations.NonNull) ExecutorService(java.util.concurrent.ExecutorService) GroupedObservable(io.reactivex.observables.GroupedObservable)

Example 2 with GroupedObservable

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);
        }
    });
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Function(io.reactivex.functions.Function) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NonNull(io.reactivex.annotations.NonNull) GroupedObservable(io.reactivex.observables.GroupedObservable)

Aggregations

NonNull (io.reactivex.annotations.NonNull)2 Function (io.reactivex.functions.Function)2 GroupedObservable (io.reactivex.observables.GroupedObservable)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Scheduler (io.reactivex.Scheduler)1 ExecutorService (java.util.concurrent.ExecutorService)1