rx.operators.OperatorParallel Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rxjava-core Show documentation
Show all versions of rxjava-core Show documentation
rxjava-core developed by Netflix
/**
* Copyright 2014 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.operators;
import rx.Observable;
import rx.Observable.Operator;
import rx.Scheduler;
import rx.Subscriber;
import rx.functions.Func1;
import rx.observables.GroupedObservable;
/**
* Identifies unit of work that can be executed in parallel on a given Scheduler.
*/
public final class OperatorParallel implements Operator {
private final Scheduler scheduler;
private final Func1, Observable> f;
private final int degreeOfParallelism;
public OperatorParallel(Func1, Observable> f, Scheduler scheduler) {
this.scheduler = scheduler;
this.f = f;
this.degreeOfParallelism = scheduler.degreeOfParallelism();
}
@Override
public Subscriber super T> call(Subscriber super R> op) {
Func1>, Subscriber super T>> groupBy =
new OperatorGroupBy(new Func1() {
long i = 0;
@Override
public Long call(T t) {
return i++ % degreeOfParallelism;
}
});
Func1>, Subscriber super GroupedObservable>> map =
new OperatorMap, Observable>(
new Func1, Observable>() {
@Override
public Observable call(GroupedObservable g) {
// Must use observeOn not subscribeOn because we have a single source behind groupBy.
// The origin is already subscribed to, we are moving each group on to a new thread
// but the origin itself can only be on a single thread.
return f.call(g.observeOn(scheduler));
}
});
// bind together Observers
return groupBy.call(map.call(new OperatorMerge().call(op)));
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy